Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. def recursive_ftp(sftp, path='.', files=None):
  2. if files is None:
  3. files = defaultdict(list)
  4.  
  5. # loop over list of SFTPAttributes (files with modes)
  6. for attr in sftp.listdir_attr(path):
  7.  
  8. if stat.S_ISDIR(attr.st_mode):
  9. # If the file is a directory, recurse it
  10. recursive_ftp(sftp, os.path.join(path,attr.filename), files)
  11.  
  12. else:
  13. # if the file is a file, add it to our dict
  14. files[path].append(attr.filename)
  15.  
  16. return files
  17.  
  18. import paramiko
  19. import stat
  20. transport = paramiko.Transport((host, port))
  21. transport.connect(username=username, password=password)
  22. sftp = paramiko.SFTPClient.from_transport(transport)
  23.  
  24. files = recursive_ftp(sftp)
  25.  
  26. /foo
  27. ----a.csv
  28. ----b.csv
  29. /bar
  30. ----c.csv
  31. /baz
  32.  
  33. {
  34. './foo': ['a.csv', 'b.csv'],
  35. './bar': ['c.csv']
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement