The Shutil module in Python

Source: Internet
Author: User

Shutil.copyfile (SRC,DST)
Both SRC and DST must be files, the source file src copied to the destination file DST, if the target file DST exists, the file content will be overwritten; no copy file src; The destination address should have write permission, the exception information thrown is IOException

>> Import Shutil
>> shutil.copyfile ("E:\python\0521.txt", "E:\0521.txt")
>> shutil.copyfile ("E:\python\1.jpg", "e:\1.jpg")

Shutil.copy (SRC,DST)
Copy a file src to DST, and the difference between shutil.copyfile () is that SRC is a file, but DST can be a directory and a file;
If the target file DST exists, the contents of the file will be overwritten; The file src will be copied, the destination address should have write permission, the exception information thrown is IOException

>> shutil.copy ("E:\python\2.txt", "C \")
>> shutil.copy ("E:\python\2.txt", "C:\3.txt")

Shutil.move (SRC,DST)
Move a file or rename a file
You can rename the file src to DST, or you can move the file src to DST

>> shutil.move ("E:\python\2.txt", "E:\python\2-2.txt")
>> shutil.move ("E:\python\2-2.txt", "C:\3-3.txt")

Shutil.copy2 (SRC,DST)
The last access time and modification time of the file are also copied on the basis of copy.

>> shutil.copy2 ("E:\python\222.txt", "C:\222.txt")

Shutil.copytree (olddir,newdir,true/false) Copy the folder and its files
Copy the Olddir to a newdir, if the 3rd parameter is true, the symbol connection under the folder will be kept when the directory is copied, and if the 3rd parameter is false, a physical copy will be generated in the replicated directory instead of the symbolic connection

>> shutil.copytree ("E:\python", "C:\python", False)
>> shutil.copytree ("E:\python", "C:\python2")

Shutil.rmtree (PATH)
Recursively delete all files in a directory and directory

>> shutil.rmtree ("C:\python")

Shutil.copymode (SRC, DST)
It just copies its permissions. Other things are not copied.

Shutil.copystat (SRC, DST)
Copy permissions, last access time, last modified time

The Shutil module in Python

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.