Python Copy file code implementation _python

Source: Internet
Author: User

The main functions are implemented in the CopyFiles () function, as follows:

Copy 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, then recursively call this function, otherwise create the recursion 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, do not duplicate the copy, otherwise 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 destination folder DST to get a list of two files, but because I need to weigh the operation, I need to 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 to a dictionary with only keys without values:

Copy 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 whether it exists in the target path, if it does not exist, first create a new path. This function is then called recursively. It's not even there. A more efficient approach is to call the Shutil.copytree () function, but it is not called because the number of files copied here needs to be computed.

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

Because the script is written primarily to sync the phone albums to the PC, simply judge the file name. To determine a different name but the same file, you can continue to judge the MD5 value, here is no longer repeat.

The complete code is as follows:

Copy Code code as follows:

#!/usr/bin/env python
#-*-Coding:utf-8-*-

# Enter two folders A and B paths, copy the files from A to B, and calculate the number of files copied. Repeat, no 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 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, then recursively call this function, otherwise create the recursion 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, do not duplicate the copy, otherwise 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 ()

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.