In Fabric how can I create a glob list from a remote path

依然范特西╮ 提交于 2021-02-11 07:20:27

问题


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

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>)]

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.


回答1:


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

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']


来源:https://stackoverflow.com/questions/53844599/in-fabric-how-can-i-create-a-glob-list-from-a-remote-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!