在Fabric中,如何从远程路径创建全局列表 [英] In Fabric how can I create a glob list from a remote path

查看:59
本文介绍了在Fabric中,如何从远程路径创建全局列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要使用Python的 Fabric ,我想与远程计算机之间进行文件传输服务器.

With Python's Fabric I want to transfer files to and from a remote server.

我需要从诸如 * *.txt 之类的全局表达式中生成要传输的文件列表(然后再应用一些其他排除项).

The list of files to transfer I need to generate from a glob expression like * or *.txt (and then apply some addional exclusions afterwards).

对于将传输到的情况,很容易遍历源文件列表,因为源是本地的:

For the case of transferring to the remote it is easy to glob the list of source files, because the source is local:

[ f for f in Path(local_dir).glob(<my glob expression>)]

但是如何在远程服务器上执行此操作?我已通过 with fabric.Connection(...)以c:与远程建立了连接,但是在连接对象中找不到 glob 方法.

But how do I do this on the remote server? I have a connection to the remote established via with fabric.Connection(...) as c:, but I cannot find a glob method in the connection object.

推荐答案

一种选择是利用SFTPClient 对象的 listdir 方法http://docs.fabfile.org/en/2.4/api/connection.html#fabric.connection.Connection.sftp"rel =" nofollow noreferrer> c.sftp() 以获得所有远程文件的列表,然后应用 fnmatch.filter 与您的glob表达式:

One option is to utilize the listdir method of the SFTPClient object returned by c.sftp() to get a listing of all remote files, and then apply fnmatch.filter with your glob expression:

fnmatch.filter(c.sftp().listdir(), '*.py')

结果:使用以下远程目录,

Result: With the following remote directory,

$ ls
1.log  2.txt  3.py  4.csv  5.py

首先列出整个目录,然后添加glob:

first listing the entire dir, then with glob:

>>> c.sftp().listdir()
['5.py', '3.py', '4.csv', '2.txt', '1.log']
>>> fnmatch.filter(c.sftp().listdir(), '*.py')
['5.py', '3.py']

这篇关于在Fabric中,如何从远程路径创建全局列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆