国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python FTP兩個文件夾間的同步實例代碼

2020-02-23 00:16:45
字體:
來源:轉載
供稿:網友

具體代碼如下所示:

# -*- 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年開始到現今               #   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'             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南雄市| 延庆县| 双城市| 江西省| 新田县| 车险| 根河市| 桦川县| 施秉县| 明光市| 德保县| 卓尼县| 潞城市| 尼玛县| 曲麻莱县| 玉田县| 永和县| 宝鸡市| 鹤山市| 清苑县| 丘北县| 昭平县| 连南| 桂阳县| 繁峙县| 绥滨县| 长顺县| 阜城县| 松阳县| 松江区| 黄梅县| 甘孜县| 昌宁县| 林周县| 沙坪坝区| 富顺县| 西宁市| 塘沽区| 通榆县| 武定县| 昌都县|