Search code examples
pythonglobremote-serverfabric

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


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.


Solution

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