Tag:ar os sp for on file log bs ad
#coding =utf-8 from ftplib import ftpimport osimport os.pathimport SHUTILIMPORT DATETIME DEF FTP_UP (filename): ftp=ftp () ftp.set_debuglevel (2) #打开调试级别2, show details; 0 To turn off debug information ftp.connect (' 10.7.39.29 ', ' 888 ') #连接 ftp.login (' Happ ', ' Happ ') #登录, if anonymous login is replaced with an empty string print ftp.getwelcome () #显示ftp服务器欢迎信息 try:   FTP.MKD (' +cyouemt ') except: print (' dir has existed %s ' % ' +cyouemt ')  FTP.CWD (' +cyouemt ') #选择操作目录 bufsize = 1024# Set buffer block size file_handler = open (filename, ' RB ') #以读模式在本地打开文件 Ftp.storbinary (' stor %s ' &Nbsp;% os.path.basename (filename), file_handler,bufsize) #上传文件 ftp.set_ DebugLevel (0) file_handler.close () ftp.quit () print "Ftp up ok" def ftp_down (HOST,PORT,USER,PASSW, Rootdir,locadir): ftp=ftp () ftp.set_debuglevel (2) ftp.connect (Host,port) ftp.login (USER,PASSW) print ftp.getwelcome () #显示ftp服务器欢迎信息 ftp.cwd (RootDir) #选择操作目录 files = ftp.nlst () for name in files: bufsize = 1024 file_handler = open (LocaDir + name, ' WB ') .write #以写模式在本地打开文Piece ftp.retrbinary (' retr %s ' % Os.path.basename (name), file_handler,bufsize) #接收服务器上文件并写入本地文件 ftp.set_debuglevel (0 ) ftp.quit () print "Ftp down ok" def copyfiles (Sourcedir, targetdir): if sourcedir.find (". SVN") > 0: return For file in os.listdir (SourceDir): Sourcefile = os.path.join (Sourcedir, file) targetfile = os.path.join (Targetdir, file) if os.path.isfile (sourcefile): if not oS.path.exists (TargetDir): os.makedirs (TargetDir) if not os.path.exists (targetfile) or (os.path.exists (targetfile) and (Os.path.getsize (targetfile) != os.path.getsize (sourcefile))): Open (targetfile, "WB"). Write (Open (sourcefile, "RB"). Read ()) if os.path.isdir (sourcefile): First_Directory = False copyfiles (sourcefile, targetfile) print ' copy them to ' +targetdir#a.coMftp_down (' 10.10.10.1 ', ' + ', ' 111 ', ' 123456 ', ' Happ ', ' toftp/') for root,dirs,files in os.walk (' Toftp '): for f in files: Ftp_up (Os.path.join (root,f)) #b. com
Python ftp upload download