Skip to content Skip to sidebar Skip to footer

List Files On Sftp Server Matching Wildcard In Python Using Paramiko

import paramiko client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect('hostname', username='test1234', password='test') path =

Solution 1:

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".

Solution 2:

Or use pysftp which is paramiko wrapper and write something like this:

import pysftp


defstore_files_name(fname):
    passdefstore_dir_name(dir_name):
    passdefstore_other_file_type(other_file):
    passwith pysftp.Connection('server', username='user', password='pass') as sftp:
    sftp.walktree('.', store_files_name, store_dir_name, store_other_file_type)

Post a Comment for "List Files On Sftp Server Matching Wildcard In Python Using Paramiko"