Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def recursive_ftp(sftp, path='.', files=None):
- if files is None:
- files = defaultdict(list)
- # loop over list of SFTPAttributes (files with modes)
- for attr in sftp.listdir_attr(path):
- if stat.S_ISDIR(attr.st_mode):
- # If the file is a directory, recurse it
- recursive_ftp(sftp, os.path.join(path,attr.filename), files)
- else:
- # if the file is a file, add it to our dict
- files[path].append(attr.filename)
- return files
- import paramiko
- import stat
- transport = paramiko.Transport((host, port))
- transport.connect(username=username, password=password)
- sftp = paramiko.SFTPClient.from_transport(transport)
- files = recursive_ftp(sftp)
- /foo
- ----a.csv
- ----b.csv
- /bar
- ----c.csv
- /baz
- {
- './foo': ['a.csv', 'b.csv'],
- './bar': ['c.csv']
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement