The main functions are implemented in the CopyFiles () function, as follows:
Copy the Code code as follows:
def copyfiles (SRC, DST):
Srcfiles = Os.listdir (src)
Dstfiles = dict (Map (Lambda x:[x, "], Os.listdir (DST)))
Filescopiednum = 0
# Copy every file in the source folder if it does not exist in the destination folder
For file in Srcfiles:
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, the function is called recursively, otherwise it will be 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:
Copy the Code code as follows:
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.
The complete code is as follows:
Copy the Code code as follows:
#!/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 of the files. Repeated non-processing.
Import OS
Import Shutil
Def copyfiles (SRC, DST):
Srcfiles = Os.listdir (src)
Dstfiles = dict (Map (Lambda x:[x, '], Os.listdir (DST)))
Filescopiednum = 0
# Copy the
for file in srcfiles:
Src_path = os.path.join (src, file) to each file in the source folder if it does 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, the recursive is created first.
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
def test ():
Src_dir = Os.path.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 created. '
Os.makedirs (Dst_dir)
num = CopyFiles (Src_dir, Dst_dir)
print ' Copy complete: ', num, ' files copied. '
if __name__ = = ' __main__ ':
Test ()