Python FTP兩個文件夾間的同步實例代碼
更新時間:2018年05月25日 10:37:13 作者:昵稱最煩了
本文通過實例代碼給大家介紹了python ftp兩個文件夾間的同步,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
具體代碼如下所示:
# -*- coding: utf-8 -*- ''''''' ftp自動檢測源文件夾的更新,將源文件夾更新的內容拷貝到目標文件夾中 使用樹的層序遍歷算法,支持深度目錄拷貝 ''' 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() # 源文件文件隊列 self.FolderListOld = [] # 目標文件文件隊列 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: # 將根目錄放入隊列中 self.FolderListOld.append(self.remotedir_old) self.FolderListNew.append(self.remotedir_new) while not (0 == len(self.FolderListOld)): end = start # 將文件夾下的文件全部壓入隊列 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)) # 確定新增文件在隊列中的位置 end += len(files) # 將已經查看的文件夾刪除 del self.FolderListOld[0] # 檢查目標文件夾該級目錄 if os.path.isdir(self.FolderListNew[0]): # 將該級目錄的文件都列出 files = os.listdir(self.FolderListNew[0]) # 檢查源文件該級目錄下的文件在目標該級目錄下是否有 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'
總結
以上所述是小編給大家介紹的Python FTP兩個文件夾間的同步實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
python3模擬百度登錄并實現(xiàn)百度貼吧簽到示例分享(百度貼吧自動簽到)
這篇文章主要介紹了python3模擬百度登錄并實現(xiàn)百度貼吧簽到示例,需要的朋友可以參考下2014-02-02Python實現(xiàn)監(jiān)視程序的內存使用情況
我們使用Python和它的數(shù)據(jù)處理庫套件進行大量數(shù)據(jù)處理時候,可能使用了大量的計算資源,那么如何監(jiān)視程序的內存使用情況就顯得尤為重要,下面我們就來了解一下具體實現(xiàn)方法吧2023-12-12詳解?PyTorch?Lightning模型部署到生產服務中
這篇文章主要為大家介紹了如何將PyTorch?Lightning模型部署到生產服務中的詳細教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09利用OpenCV+Tensorflow實現(xiàn)的手勢識別
這幾天沒事,想著再學點一些視覺識別方向的東西,因為之前做了驗證碼識別,有了機器學習的信心,因此這次打算做個手勢識別,下面這篇文章主要給大家介紹了關于利用OpenCV+Tensorflow實現(xiàn)的手勢識別的相關資料,需要的朋友可以參考下2022-11-11