Python FTP兩個(gè)文件夾間的同步實(shí)例代碼
具體代碼如下所示:
# -*- coding: utf-8 -*- ''''''' ftp自動(dòng)檢測(cè)源文件夾的更新,將源文件夾更新的內(nèi)容拷貝到目標(biāo)文件夾中 使用樹(shù)的層序遍歷算法,支持深度目錄拷貝 ''' 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'開(kāi)始連接到 %s' %(hostaddr) ftp.connect(hostaddr) print u'成功連接到 %s' %(hostaddr) print u'開(kāi)始登錄到 %s' %(hostaddr) ftp.login(username, password) print u'成功登錄到 %s' %(hostaddr) ftp.getwelcome() except Exception, e: print 'find exception now:',e # 使用樹(shù)的層序遍歷來(lái)檢查文件目錄 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)文件夾該級(jí)目錄 if os.path.isdir(self.FolderListNew[0]): # 將該級(jí)目錄的文件都列出 files = os.listdir(self.FolderListNew[0]) # 檢查源文件該級(jí)目錄下的文件在目標(biāo)該級(jí)目錄下是否有 for file in self.FolderListOld[start:end]: temp = file.split('\\') if temp[-1] in files: # 這里判斷文件大小是否一致,不一致拷過(guò)去 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' # # 如果是文件夾存在,但是修改過(guò),沒(méi)有必要全部拷貝文件夾,可以到文件夾中拷貝單個(gè)文件 # 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) #以毫秒為單位的時(shí)間,自1970年開(kāi)始到現(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): # 如果是文件夾不存在使用,目錄樹(shù)拷貝 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兩個(gè)文件夾間的同步實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
python3模擬百度登錄并實(shí)現(xiàn)百度貼吧簽到示例分享(百度貼吧自動(dòng)簽到)
這篇文章主要介紹了python3模擬百度登錄并實(shí)現(xiàn)百度貼吧簽到示例,需要的朋友可以參考下2014-02-02Python實(shí)現(xiàn)監(jiān)視程序的內(nèi)存使用情況
我們使用Python和它的數(shù)據(jù)處理庫(kù)套件進(jìn)行大量數(shù)據(jù)處理時(shí)候,可能使用了大量的計(jì)算資源,那么如何監(jiān)視程序的內(nèi)存使用情況就顯得尤為重要,下面我們就來(lái)了解一下具體實(shí)現(xiàn)方法吧2023-12-12python按行讀取文件,去掉每行的換行符\n的實(shí)例
下面小編就為大家分享一篇python按行讀取文件,去掉每行的換行符\n的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04詳解?PyTorch?Lightning模型部署到生產(chǎn)服務(wù)中
這篇文章主要為大家介紹了如何將PyTorch?Lightning模型部署到生產(chǎn)服務(wù)中的詳細(xì)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09利用OpenCV+Tensorflow實(shí)現(xiàn)的手勢(shì)識(shí)別
這幾天沒(méi)事,想著再學(xué)點(diǎn)一些視覺(jué)識(shí)別方向的東西,因?yàn)橹白隽蓑?yàn)證碼識(shí)別,有了機(jī)器學(xué)習(xí)的信心,因此這次打算做個(gè)手勢(shì)識(shí)別,下面這篇文章主要給大家介紹了關(guān)于利用OpenCV+Tensorflow實(shí)現(xiàn)的手勢(shì)識(shí)別的相關(guān)資料,需要的朋友可以參考下2022-11-11pygame游戲之旅 調(diào)用按鈕實(shí)現(xiàn)游戲開(kāi)始功能
這篇文章主要為大家詳細(xì)介紹了pygame游戲之旅的第12篇,教大家調(diào)用按鈕實(shí)現(xiàn)游戲開(kāi)始功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11