Python FTP Synchronization instance code between two folders

  • 2020-09-28 08:58:26
  • OfStack

The specific code is as follows:


# -*- coding: utf-8 -*- 
''''''' 
  ftp Automatically detects source folder updates and copies the source folder updates to the target folder  
   Using hierarchical traversal algorithm of tree, support deep directory copy  
''' 
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() 
    #  Source file queue  
    self.FolderListOld = [] 
    #  Object file file queue  
    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' Start connecting to  %s' %(hostaddr) 
      ftp.connect(hostaddr) 
      print u' Successfully connected to  %s' %(hostaddr) 
      print u' Start logging in to  %s' %(hostaddr) 
      ftp.login(username, password) 
      print u' Successfully logged in  %s' %(hostaddr) 
      ftp.getwelcome() 
    except Exception, e: 
      print 'find exception now:',e 
  #  Use the hierarchical traversal of the tree to check the file directory  
  def LevelOrderFolder(self): 
    #  Add file start and end locations  
    start = 0 
    end = 0 
    try: 
      #  Put the root directory into the queue  
      self.FolderListOld.append(self.remotedir_old) 
      self.FolderListNew.append(self.remotedir_new) 
      while not (0 == len(self.FolderListOld)): 
        end = start 
        #  Press all the files under the folder into the queue  
        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)) 
          #  Determines the location of the new file in the queue  
          end += len(files) 
        #  Delete the folder that you have viewed  
        del self.FolderListOld[0] 
        #  Check the target folder for this level directory  
        if os.path.isdir(self.FolderListNew[0]): 
          #  List all the files in that level directory  
          files = os.listdir(self.FolderListNew[0]) 
          #  Check that the files in the source directory are in the target directory  
          for file in self.FolderListOld[start:end]: 
            temp = file.split('\\') 
            if temp[-1] in files: 
              #  Here, determine if the file size is 1 , don't 1 Kao to the past  
              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 a folder exists, but has been modified, it is not necessary to copy all folders, you can copy a single file into the folder  
              # 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) # Time in milliseconds since 1970 The year began till now  
              #   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): 
                #  If the folder is not used, copy the directory tree  
                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: 
                #  If it's a file  
                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__': 
  #  Configure the following variables  
  hostaddr = r'10.204.16.28' # ftp address  
  username = r' ' #  The user name  
  password = r' ' #  password  
  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' 

conclusion


Related articles: