Guest User

Untitled

a guest
Sep 5th, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. Python-FTP download all files in directory
  2. from ftplib import FTP
  3. import os, sys, os.path
  4.  
  5. def handleDownload(block):
  6. file.write(block)
  7. print ".",
  8.  
  9. ddir='C:\Data\test\'
  10. os.chdir(ddir)
  11. ftp = FTP('test1/server/')
  12.  
  13. print 'Logging in.'
  14. ftp.login('user1\anon', 'pswrd20')
  15. directory = '\data\test\'
  16.  
  17. print 'Changing to ' + directory
  18. ftp.cwd(directory)
  19. ftp.retrlines('LIST')
  20.  
  21. print 'Accessing files'
  22.  
  23. for subdir, dirs, files in os.walk(directory):
  24. for file in files:
  25. full_fname = os.path.join(root, fname);
  26. print 'Opening local file '
  27. ftp.retrbinary('RETR C:\Data\test\' + fname,
  28. handleDownload,
  29. open(full_fname, 'wb'));
  30. print 'Closing file ' + filename
  31. file.close();
  32. ftp.close()
  33.  
  34. cd c:destination
  35. wget --mirror --continue --no-host-directories --user=username --password=s3cr3t ftp://hostname/source/path/
  36.  
  37. for subdir, dirs, files in os.walk(directory):
  38.  
  39. >>> class c:
  40. ... def __call__(self, *args):
  41. ... print(args)
  42. ...
  43. >>> f = c()
  44. >>> f('hello')
  45. ('hello',)
  46. >>> f('hello', 'world')
  47. ('hello', 'world')
  48.  
  49. class handle_lines:
  50. def __init__(self):
  51. self.lines = []
  52. def __call__(self, *args):
  53. self.lines << args[0]
  54.  
  55. filenames = []
  56. ftp.retrlines('NLST', filenames.append)
  57. print filenames
  58.  
  59. for filename in filenames:
  60. local_filename = os.path.join('C:\test\', filename)
  61. file = open(local_filename, 'wb')
  62. ftp.retrbinary('RETR '+ filename, file.write)
  63.  
  64. file.close()
  65.  
  66. ftp.close()
Add Comment
Please, Sign In to add comment