Python shutil Module

Source: Internet
Author: User

Step 1: manual Introduction

Shutil -- high-level file operations is a high-level file operation tool.

Similar to the advanced API, and its major strength lies in its support for file copying and deletion operations.

Related API Introduction

 
Copyfile (SRC, DST)

 

Copy the source SRC to DST. Of course, the premise is that the target address has the write permission. The thrown exception information is

Ioexception. If the current DST already exists, it will be overwritten.

NOTE: Special files such as character or block devices and pipes cannot be copied with this function. I don't understand the meaning of this sentence. Can I read or write the hard disk?

 
Copyfileobj (

FSRC, fdst [, length])

Copy the contents of the file-like object FSRC to the file-like object fdst. The integer length,IfGiven,IsThe buffer size.

Copymode (

SRC, DST)

 

Copy the permission bits from SRC to DST. The file contents, owner, and group are unaffected. SRC and DST are path names given as strings.

Meaning: It only copies its permissions. Other things won't be copied.

Copystat (SRC, DST)

Copy the permission bits, last access time, and last modification time from SRC to DST. The file contents, owner, and group are unaffected. SRC and DST are path names given as strings.

Copy permission, last access time, last modification time

Copy (SRC, DST)

Copy the file SRC to the file or directory DST. if DST is a directory, a file with the same basename as SRC is created (or overwritten) in the directory specified. permission bits are copied. SRC and DST are path names given as strings.

Copy an object to a file or directory

Copy2 (SRC, DST)

Similar to copy (), but last access time and last modification time are copied as well. This is similar to the Unix Command CP-P.

On the basis of copy, the last access time and modification time of the copied file are also copied.

Something similar to CP-P

Rmtree (path [, ignore_errors [, onerror])

Delete an entire directory tree (path must point to a directory ). if ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.

If onerror is provided, it must be a callable that accepts three parameters: function, path, and excinfo. the first parameter, function, is the function which raised the exception; it will be OS. listdir (), OS. remove () or OS. rmdir (). the second parameter, path, will be the path name passed to function. the third parameter, excinfo, will be the exception information return by sys. exc_info (). exceptions raised by onerror will not be caught.

Move (SRC, DST)

Recursively move a file or directory to another location.

If the destination is on our current filesystem, then simply use rename. Otherwise, copy SRC to the DST and then remove SRC.

Note: If the file systems in the two locations are the same, the RENAME operation is equivalent. If the file systems are not the same, the move operation is performed!

Module learning Step 2: Instances

Copy an object

ImportOS, String, sys, time, Re, math, fileinput, glob, shutil

PrintOS. listdir ('.')

ForFileInOS. listdir ('.'):

IfOS. Path. splitext (File) [1] =". Py":

PrintFile

Shutil. Copy (file,"A. py")

 

Delete a directory

Shutil. rmtree ("Te")

 

 Copyfile (SRC, DST) Copy the source SRC to DST. Of course, the premise is that the target address has the write permission. Ioexception is thrown. If the current DST already exists, it will be overwritten.
 Copymode (SRC, DST) It will only copy its permissions. Other things will not be copied.
 Copystat (SRC, DST) Copy permission, last access time, last modification time
 Copy (SRC, DST)  Copy an object to a file or directory
Copy2 (SRC, DST)  On the basis of copy, the last access time and modification time of the file are also copied, similar to CP-P.
 Copy2 (SRC, DST) If the file system in the two locations is the same, it is equivalent to the RENAME operation, but it is renamed. If the file system is not the same, it is the move operation.
copytree (olddir, newdir, true/flase) copy olddir into newdir. If the 3rd parameters are true, when the directory is copied, the symbolic connection under the folder will be maintained. If the 3rd parameter is false, A physical copy is generated under the copied directory to replace the symbolic connection.

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.