您好,登錄后才能下訂單哦!
具體代碼如下所示:
# -*- coding: utf-8 -*- ''''''' ftp自動檢測源文件夾的更新,將源文件夾更新的內(nèi)容拷貝到目標(biāo)文件夾中 使用樹的層序遍歷算法,支持深度目錄拷貝 ''' import os from ftplib import FTP import os,sys,string,datetime,time import shutil import socket class MyUpdateMonitor(object): def __init__(self, hostaddr, username, password, remotedir_old, remotedir_new, port = 21): self.hostaddr = hostaddr self.username = username self.password = password self.remotedir_old = remotedir_old self.remotedir_new = remotedir_new # self.port = port self.ftp = FTP() # 源文件文件隊(duì)列 self.FolderListOld = [] # 目標(biāo)文件文件隊(duì)列 self.FolderListNew = [] def __del__(self): self.ftp.close() self.FolderListOld.clear() self.FolderListNew.clear() def FtpLogin(self): ftp = self.ftp try: timeout = 300 socket.setdefaulttimeout(timeout) ftp.set_pasv(True) print u'開始連接到 %s' %(hostaddr) ftp.connect(hostaddr) print u'成功連接到 %s' %(hostaddr) print u'開始登錄到 %s' %(hostaddr) ftp.login(username, password) print u'成功登錄到 %s' %(hostaddr) ftp.getwelcome() except Exception, e: print 'find exception now:',e # 使用樹的層序遍歷來檢查文件目錄 def LevelOrderFolder(self): # 新增文件起始位置和終止位置 start = 0 end = 0 try: # 將根目錄放入隊(duì)列中 self.FolderListOld.append(self.remotedir_old) self.FolderListNew.append(self.remotedir_new) while not (0 == len(self.FolderListOld)): end = start # 將文件夾下的文件全部壓入隊(duì)列 if os.path.isdir(self.FolderListOld[0]): files = os.listdir(self.FolderListOld[0]) for file in files: self.FolderListOld.append(os.path.join(self.FolderListOld[0], file)) # 確定新增文件在隊(duì)列中的位置 end += len(files) # 將已經(jīng)查看的文件夾刪除 del self.FolderListOld[0] # 檢查目標(biāo)文件夾該級目錄 if os.path.isdir(self.FolderListNew[0]): # 將該級目錄的文件都列出 files = os.listdir(self.FolderListNew[0]) # 檢查源文件該級目錄下的文件在目標(biāo)該級目錄下是否有 for file in self.FolderListOld[start:end]: temp = file.split('\\') if temp[-1] in files: # 這里判斷文件大小是否一致,不一致拷過去 if os.path.isfile(file) and not os.path.getsize(file) == os.path.getsize(os.path.join(self.FolderListNew[0], temp[-1])): print 'Find the file(%s) size is changed!\n' % file # print r'Start delete...\n' # os.remove(os.path.join(self.FolderListNew[0], temp[-1])) # print r'delete is over...\n' print 'Start Copy...\n' shutil.copyfile(file, os.path.join(self.FolderListNew[0], temp[-1])) print 'Copy is over...\n' # # 如果是文件夾存在,但是修改過,沒有必要全部拷貝文件夾,可以到文件夾中拷貝單個文件 # if os.path.isfile(file) and not (os.path.getmtime(file) == os.path.getmtime(os.path.join(self.FolderListNew[0], temp[-1]))): # print 'Find the file(%s) size is changed!\n' % file # changetime = os.path.getmtime(file) #以毫秒為單位的時間,自1970年開始到現(xiàn)今 # changetime = float(changetime) # print 'Change Time is', time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(changetime)), r'\n' # # print 'Start Copy...\n' # shutil.copyfile(file, os.path.join(self.FolderListNew[0], temp[-1])) # print 'Copy is over...\n' else: if os.path.isdir(file): # 如果是文件夾不存在使用,目錄樹拷貝 print 'Find the folder(%s) is updated!\n' % file print 'Start Copy...\n' shutil.copytree(file, os.path.join(self.FolderListNew[0], temp[-1])) print 'Copy is over...\n' else: # 如果是文件 print 'Find the file(%s) is updated!\n' % file print 'Start Copy...\n' shutil.copyfile(file, os.path.join(self.FolderListNew[0], temp[-1])) print 'Copy is over...\n' self.FolderListNew.append(os.path.join(self.FolderListNew[0], temp[-1])) del self.FolderListNew[0] start = end - 1 except Exception, e: print 'find exception now:',e if __name__ == '__main__': # 配置如下變量 hostaddr = r'10.204.16.28' # ftp地址 username = r' ' # 用戶名 password = r' ' # 密碼 remotedir_old = r'\\10.204.16.28\Home\TDME\Test\Old\TMUMH_1.6.1055' remotedir_new = r'\\10.204.16.28\Home\TDME\Test\New\TMUMH_1.6.1055' monitorfileupdae = MyUpdateMonitor(hostaddr, username, password, remotedir_old, remotedir_new) monitorfileupdae.FtpLogin() while True: print 'Start Check Update...\n' monitorfileupdae.LevelOrderFolder() print 'Check Update is Over...\tSleep one hour...' time.sleep(3600) print 'hello'
總結(jié)
以上所述是小編給大家介紹的Python FTP兩個文件夾間的同步實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。