Python-file copy

Source: Internet
Author: User
Manual operations are troublesome when you back up photos on your mobile phone recently. you just want to write a script to automatically perform the photos on your mobile phone, I want to write a script automatically. Because some photos have been backed up before the backup, you need to make a heavy decision.

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 # if each file in the source folder does not exist in the target folder, copy 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 target folder, this function is called recursively; otherwise, it is created first and then 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 if it is not repeated; otherwise, no operation is performed. 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 OS. the listdir () function is used to retrieve the src folder and the dst folder of the source folder to obtain the list of two files. However, because I need to perform the deduplication operation, I need to query the files in the dst file list. Because the list query efficiency is not high, and the dictionary is a hash table, the query efficiency is high, so I will convert 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 check whether the folder exists in the target path. if the path does not exist, create a new path. Then recursively call this function. The shutil. copytree () function is called more efficiently when it does not exist. However, the shutil. copytree () function is not called because the number of copied files needs to be calculated here.

If the path is a file, first determine whether the file exists in the target folder. If it does not exist, copy it.

Because this script is mainly used to synchronize the mobile album to the PC, you can simply judge the file name. To identify different files with the same names, you can continue to judge the md5 value.

#! /Usr/bin/env python #-*-coding: UTF-8-*-# enter two folders a and B path, copy the files in a to B, and calculate the number of copied files. Duplicate requests are not processed. # Pythontab.com 2013-07-19import osimport shutil def copyFiles (src, dst): srcFiles = OS. listdir (src) dstFiles = dict (map (lambda x: [x, ''], OS. listdir (dst) filesCopiedNum = 0 # if each file in the source folder does not exist in the target folder, copy 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 target folder, this function is called recursively; otherwise, it is created first and then 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 if it is not repeated; otherwise, no operation is performed. 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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.