Home > Article > Backend Development > Python - file copy
When I was backing up photos on my phone recently, I found it a bit troublesome to do it manually, so I wanted to write a script to do it automatically. Because some photos have been backed up before, a duplication judgment operation is required.
The main function is 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 # 对源文件夹中的每个文件若不存在于目的文件夹则复制 for file in srcFiles: src_path = os.path.join(src, file) dst_path = os.path.join(dst, file) # 若源路径为文件夹,若存在于目标文件夹,则递归调用本函数;否则先创建再递归。 if os.path.isdir(src_path): if not os.path.isdir(dst_path): os.makedirs(dst_path) filesCopiedNum += copyFiles(src_path, dst_path) # 若源路径为文件,不重复则复制,否则无操作。 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, and get two file lists, but because I need to judge Heavy operation, so query operation needs to be performed in the dst file list. Since the query efficiency of the list is not high, and the dictionary is a hash table, the query efficiency is high, so I converted the target file list into a dictionary with only keys and no values:
dstFiles = dict(map(lambda x:[x , ''], os.listdir(dst)))
Then I traverse the source file list. If the path is a folder, first determine whether the folder exists in the target path. If it does not exist, create one first. new path. Then call this function recursively. In fact, when it does not exist, a more efficient method is to call shutil.copytree() function, but since the number of copied files needs to be calculated here, this function is not called.
If the path is a file, first determine whether the file exists in the target folder. If it does not exist, copy it.
Since this script is mainly written to synchronize the mobile photo album to the PC, it only briefly determines the file name. If you want to judge files with different names but the same, you can continue to judge the md5 value, which will not be described here.
#!/usr/bin/env python # -*- coding: UTF-8 -*- # 输入两个文件夹a和b路径,将a中的文件拷进b,并计算拷贝的文件数。重复的不作处理。 # pythontab.com 2013-07-19 import os import shutil def copyFiles(src, dst): srcFiles = os.listdir(src) dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst))) filesCopiedNum = 0 # 对源文件夹中的每个文件若不存在于目的文件夹则复制 for file in srcFiles: src_path = os.path.join(src, file) dst_path = os.path.join(dst, file) # 若源路径为文件夹,若存在于目标文件夹,则递归调用本函数;否则先创建再递归。 if os.path.isdir(src_path): if not os.path.isdir(dst_path): os.makedirs(dst_path) filesCopiedNum += copyFiles(src_path, dst_path) # 若源路径为文件,不重复则复制,否则无操作。 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 will be created.' os.makedirs(dst_dir) num = copyFiles(src_dir, dst_dir) print 'Copy complete:', num, 'files copied.' if __name__ == '__main__': test()