When I recently backed up the photos on my phone, I felt a bit of a hassle with purely manual operation and wanted to write a script to do it automatically. Some photos have been backed up before, so there is a need for a weight-back operation.
The main functions are implemented in the CopyFiles () function, as follows:
def copyfiles (SRC, DST): srcfiles = Os.listdir (src) dstfiles = dict (Map (Lambda x:[x, "], Os.listdir (DST))) filescopiednum = 0 # Copies the for file in srcfiles: src_path = os.path.join (src, file) to each of the files in the source folder if they do not exist in the destination folder Dst_path = os.path.join (DST, file) # If the source path is a folder, if it exists in the destination folder, the function is called recursively, otherwise it is first created recursively. if Os.path.isdir (src_path): if not Os.path.isdir (Dst_path): os.makedirs (Dst_path) Filescopiednum + = CopyFiles (Src_path, Dst_path) # If the source path is a file, copy it without repeating it, otherwise there is no action. elif Os.path.isfile (src_path): if not Dstfiles.has_key (file): shutil.copyfile (Src_path, Dst_path) Filescopiednum + = 1 return filescopiednum
Here I first use the Os.listdir () function to traverse the source folder src and the target folder DST, to get two file lists, but since I need to work on the re-operation, I need to make a query in the DST file list. Because the query efficiency of the list is not high, and the dictionary is a hash table, the query is more efficient, so I convert the list of target files into a dictionary with only keys and no values:
Dstfiles = dict (Map (Lambda x:[x, "], Os.listdir (DST)))
Then I traverse the list of source files, if the path is a folder, first determine if it exists in the target path, if it does not exist, create a new path first. The function is then called recursively. It is more efficient to call the Shutil.copytree () function when it does not exist, but the function is not called because there is a need to calculate the number of copies of the file here.
If the path is a file, first determine if the file exists in the destination folder. If it does not exist, copy it.
Because this script is written mainly to synchronize the phone album to the PC, so simply to determine the file name. To determine a different name but the same file, you can continue to judge the MD5 value, here will not repeat.
#!/usr/bin/env python#-*-coding:utf-8-*-# Enter two folders A and B paths, copy the files in a into B, and calculate the number of copies. Repeated non-processing. # pythontab.com 2013-07-19import osimport shutil def copyfiles (SRC, DST): Srcfiles = Os.listdir (src) dstfiles = dic T (Map (Lambda x:[x, "], Os.listdir (DST))) Filescopiednum = 0 # Copy the for file in Srcfile if the destination folder does not exist in the source folder S:src_path = Os.path.join (src, file) Dst_path = Os.path.join (DST, file) # If the source path is a folder, if it exists in the destination folder, then recursive call to this letter First, or create a recursive return. If Os.path.isdir (Src_path): If not Os.path.isdir (Dst_path): Os.makedirs (Dst_path) fi Lescopiednum + = CopyFiles (Src_path, Dst_path) # If the source path is a file, copy it without repeating it, otherwise there is no action. Elif Os.path.isfile (Src_path): If not Dstfiles.has_key (file): Shutil.copyfile (src _path, Dst_path) Filescopiednum + = 1 return filescopiednum def Test (): Src_dir = OS.PA Th.abspath (raw_input (' Please enter the source path: ')) if not os.path.isDir (src_dir): print ' Error:source folder does not exist! ' return 0 Dst_dir = Os.path.abspath (raw_input (' Please enter the destination path: ')) if Os.path.isdir (Dst_dir) : num = CopyFiles (Src_dir, Dst_dir) else:print ' Destination folder does not exist, a new one would be CRE Ated. ' Os.makedirs (dst_dir) num = CopyFiles (src_dir, dst_dir) print ' Copy complete: ', num, ' files copied. ' if __name__ = = ' __main__ ': Test ()