The current code will load the deleted XML files in the directory where this program is located. How to specify a different local directory as the destination?
Please also tell me if there is any weird code here. :)
import ftplib import os import os import socket HOST = 'ftp.server.com' DIRN = 'DirectoryInFTPServer' filematch = '*.xml' username = 'username' password = 'password' def main(): try: f = ftplib.FTP(HOST) except (socket.error, socket.gaierror), e: print 'ERROR: cannot reach "%s"' % HOST return print '*** Connected to host "%s"' % HOST try: f.login(username, password) except ftplib.error_perm, e: print 'ERROR: cannot login' f.quit return print '*** Logged in successfully' try: f.cwd(DIRN) except ftplib.error_perm, e: print 'ERROR: cannot CD to "%s"' % DIRN f.quit() print '*** Changed to folder: "%s"' % DIRN try: s = 0; for filename in f.nlst(filematch): fhandle = open(filename, 'wb') print 'Getting ' + filename f.retrbinary('RETR ' + filename, fhandle.write) s = s + 1 except ftplib.error_perm, e: print 'ERROR: cannot read file "%s"' % filename os.unlink(filename) f.quit() print 'Files downloaded: ' + str(s) return if __name__ == '__main__': main()
source share