Python-Python tutorial

Source: Internet
Author: User
When I was backing up photos on my mobile phone recently, it was a little troublesome to perform manual operations. I wanted to write a script for automatic operation. Because some photos have been backed up before the backup, you need to perform a severe operation. See the following implementation code to implement the main functions in the copyFiles () function, as shown below:

The code is as follows:


Def copyFiles (src, dst ):
SrcFiles = OS. listdir (src)
DstFiles = dict (map (lambda x: [x, ''], OS. listdir (dst )))
FilesCopiedNum = 0

# Copy each file in the source folder if it does not exist in the target 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, this function is called recursively if it exists in the target folder; 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:

The code is as follows:


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.

The complete code is as follows:

The code is as follows:


#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-

# Enter the paths a and B of the two folders, copy the files in a to B, and calculate the number of copied files. Duplicate requests are not processed.

Import OS
Import shutil

Def copyFiles (src, dst ):
SrcFiles = OS. listdir (src)
DstFiles = dict (map (lambda x: [x, ''], OS. listdir (dst )))
FilesCopiedNum = 0

# Copy each file in the source folder if it does not exist in the target 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, this function is called recursively if it exists in the target folder; 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.