List files on SFTP server matching wildcard in Python using Paramiko

The glob will not magically start working with a remote server, just because you have instantiated SSHClient before.

You have to use Paramiko API to list the files, like SFTPClient.listdir:

import fnmatch
sftp = client.open_sftp()

for filename in sftp.listdir('/home/test'):
    if fnmatch.fnmatch(filename, "*.txt"):
        print filename

You can also use a regular expression for the matching, if it suits your needs better. See Using wildcard in remote path using Paramiko’s SFTPClient.


Side note: Do not use AutoAddPolicy. You
lose security by doing so. See Paramiko “Unknown Server”
.

Leave a Comment