python detects folder changes and copies updated files to the corresponding directory

  • 2020-12-20 03:42:14
  • OfStack

Check the folder and copy the updated files to the corresponding directory

Pro test is available. If there is any reference, please modify the file path;

Learning python 1 month after writing this function, belongs to beginners, if there is a god passing by, code optimization ~

newcopy. py:

Detect the last modified file in the folder, copy and copy it to the corresponding path, and the copy directory will be automatically detected and output; Test folder path remember to modify;

pyinotify. py:

Using the window interface, detect folder changes (update, delete, add, etc.) in the directory where the script is located, and output the log to the desktop;


# newcopy.py file 
# -*- coding:UTF-8 -*-
import os
import os.path
import sys
import time
import datetime
import stat
import difflib
import linecache, shutil

#  The file full path and corresponding last modified time is written to out.txt In the document; 
def add_log(path):
 with open('out.txt','w') as f:
  f.close()
 for root , dirs, files in os.walk(path):
  for name in files:
   temp_path = os.path.join(root,name)
   file_name = temp_path.replace('C:/Users/Enter/Desktop/', '')
   file_time = os.stat(temp_path).st_mtime
   with open('out.txt','a') as f:
    f.write( ','.join( ['%s' % file_name , '%s\n' % file_time] ) )
    f.close()

 #  Note the time format conversion 
   #file_time = time.localtime(os.stat(root).st_mtime)
   #file_time=date.strftime('%Y-%m-%d %H:%M:%S')

def if_exist():

 #  Judge documents out.txt If there is a , Create if it doesn't exist 
 filename = 'out.txt'
 if os.path.exists(filename):
  message = 'OK, the "%s" file exists.'
 else:
  message = "Sorry, I cannot find the '%s' file..and I create it."
  a = open('out.txt', 'w')
  a.close()
 print message % filename

 #  judge update Whether the folder exists , Create if it doesn't exist 
 files_name='update'
 if os.path.exists(files_name):
  message = 'OK, the "%s" file exists.'
 else:
  message = "Sorry, I cannot find the '%s' file.and I create it. "
  os.mkdir('update')
 print message % files_name


# path  The folder path to compare 
#  Return the generated txt The path that contains the updated or added file path 
def log_compare(path):

 #  To ensure that out.txt There are 
 if_exist()

 #  To obtain out.txt File contents (file full path key And the last modification time value Generated), dict
 txt = open('out.txt', 'r').readlines()
 myDic = {}
 for row in txt:
  (key, value) = row.split(',')
  myDic[key] = value
 print myDic

 #  Create files and folders named after the time 
 setup_filename = str(datetime.datetime.now().strftime('%Y%m%d%H%M%S'))    #  Get current time 
 setup_file_path = '%s%s.txt' %('C:/Users/Enter/Desktop/update/' ,setup_filename) #  Generates one named after the current time .txt File, ready to write to the update log 
 setup_file_dir = '%s%s' %('C:/Users/Enter/Desktop/update/' ,setup_filename)  #  Generates one named after the current time .txt folder 

 # judge key And compare the value Does the value change 
 # Original need has 1 a out.txt Documents can be compared value Determine if there are updates 
 # When you run the program, re-iterate 1 Full path through the file and last modification time 
 for root , dirs, files in os.walk(path):
  for name in files:
   temp_path = os.path.join(root,name)
   file_name = temp_path.replace('C:/Users/Enter/Desktop/', '')
   time = os.stat(temp_path).st_mtime        #  Gets the last modification time 
   file_time = '%s\n' % time          #  add %s\n Is in order to out.txt The inside values are exactly the same 
   if myDic.has_key(file_name) == True:
    if cmp(myDic[file_name], file_time):  # myDic[file_name] The old last revision date, file_time New last modified time 
     print (file_name,file_time)    #  Outputs the file name with the change and the corresponding last modification time 

     #  Output the update log named after the file time. The build path is update Under the 
     with open(setup_file_path,'a') as f: #  An updated file is written to the update log 
      f.write( '%s\n' % file_name )
      f.close()
   else:
    print "add",file_name
    with open(setup_file_path,'a') as f:   #  A new file is added to the update log 
      f.write( '%s\n' % file_name )
      f.close()

  #  return   Current time, folder path named by time, update file path 
 return (setup_filename, setup_file_dir, setup_file_path)

#  will src Copy the contents of the directory to dest directory 
#  if dest If the subdirectory does not exist, create it first 
# txt_path To update the log path, the updated file is copied 
def copy_directory(src, dest, txt_path):
 if not os.path.exists(txt_path):
  print "no file update"
  return

 #  Read the update log to get the full path to the update file 

 txt = open(txt_path, 'r').readlines()
 myDic = {}
 myDic2 = {}
 for row in txt:
  myDic[row] = "1"
  tempArray = os.path.split(row)
  key = tempArray[0]
  myDic2[key] = "1"

 print "myDic2:", myDic2
 print "dict:", myDic

 #  Traverse the original folder to get the full path to all files 
 for root, dirs, files in os.walk(src):
  for name in files:
   #print "dirs:",dirs
   fpath = os.path.join(root, name)
   newroot = root
   newroot = newroot.replace(src, dest)  #  According to the absolute path of the file, create the path to be copied (relative path), create no 
   #print newroot
   rel_dir = root.replace('C:/Users/Enter/Desktop/', '')
   if not os.path.exists(newroot) and myDic2.has_key(rel_dir):
    print "rel_dir:" , rel_dir
    print newroot
    os.makedirs(newroot)
    os.chmod(newroot, stat.S_IWRITE)
   temp = fpath
   temp = temp.replace(src, dest)
   rel_path = fpath.replace('C:/Users/Enter/Desktop/', '')  #  Change the absolute path to the relative path to make it easier to walk through the comparison and pick out the files to copy 
   rel_path += '\n'

   if myDic.has_key(rel_path) == True:
    print "real_path:" , rel_path
    # os.mkdir(rel_path)
    shutil.copy(fpath, temp)
    print "copyfile:", fpath


def main():

 path_dir = 'C:/Users/Enter/Desktop/acd'
 path_file = 'C:/Users/Enter/Desktop/out.txt'

 params = log_compare(path_dir)
 add_log(path_dir)
 copy_directory(path_dir, params[1], params[2])


if __name__ == '__main__':
 main()

#pyinotify.py file 
# -*- coding:UTF-8 -*-
import os
import win32file
import win32con
# # Detect all file deletion, update, modification and other changes in the current directory. Update log output to desktop. 2016.5.23 copy


ACTIONS = {
 1 : "Created",
 2 : "Deleted",
 3 : "Updated",
 4 : "Renamed from something",
 5 : "Renamed to something"
}
# Thanks to Claudio Grondi for the correct set of numbers
FILE_LIST_DIRECTORY = 0x0001
path_to_watch = "."
hDir = win32file.CreateFile (
 path_to_watch,
 FILE_LIST_DIRECTORY,
 win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
 None,
 win32con.OPEN_EXISTING,
 win32con.FILE_FLAG_BACKUP_SEMANTICS,
 None
)
while 1:
 #
 # ReadDirectoryChangesW takes a previously-created
 # handle to a directory, a buffer size for results,
 # a flag to indicate whether to watch subtrees and
 # a filter of what changes to notify.
 #
 # NB Tim Juchcinski reports that he needed to up
 # the buffer size to be sure of picking up all
 # events when a large number of files were
 # deleted at once.
 #
 results = win32file.ReadDirectoryChangesW (
 hDir,
 1024,
 True,
  win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
  win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
  win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
  win32con.FILE_NOTIFY_CHANGE_SIZE |
  win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
  win32con.FILE_NOTIFY_CHANGE_SECURITY,
 None,
 None
 )

 #print "results:", results

 for action, file in results:
 full_filename = os.path.join (path_to_watch, file)
 print full_filename, ACTIONS.get (action, "Unknown")
 with open('C:/Users/Enter/Desktop/fileupdate.txt','a') as f:
  #str = ','.join( ['%s' % full_filename , '%s\n' % ACTIONS.get (action, "Unknown")] )
  #print str
  f.write( ','.join( ['%s' % full_filename , '%s\n' % ACTIONS.get (action, "Unknown")] ) )
  f.close()

Related articles: