Python version 2.7
Code:
#-*-coding:utf-8-*-Import OS import time from ftplib import FTP #服务器地址 ftp_server= ' xx.xx.xx.xx '--corresponding FTPE server address USER = ' user ' PWD = ' password ' ftp_path= '/home/xx/xx/' local_root= ' e: ' +ftp_path date= (' Time.strftime ',%y%m%d (
Time.time ()) Print DATE def isdir (filename): Try:path = filename; Path.replace ('/', ' \ \ ') if Os.path.exists (path): print '---file exists--' else:pri NT ' File not exists ', Local_root os.mkdirs (local_root) return True Except:return
False def ftpconnect (): Ftp=ftp () ftp.set_debuglevel (2) ftp.connect (ftp_server,21) ftp.login (USER,PWD) return ftp def downloadfile (): FTP = Ftpconnect () print ftp.getwelcome () #显示ftp服务器欢迎信息 li = ftp.nl St (ftp_path) print ' FTP: ', Li for eachfile in li:localpath = ' e: ' +eachfile print '--open Localpa
Th--', localpath bufsize = 1024 Isdir (localpath) fp = open (LocalPath, ' wb+ ') ftp.retrbinary (' RETR ' +eachfile,fp.write,bufsize) Fp.flush () ftp.se T_debuglevel (0) #关闭调试 fp.close () ftp.quit () #退出ftp服务器 if __name__== "__main__": DownloadFile ()