Shutil module and pythonshutil Module

Source: Internet
Author: User

Shutil module and pythonshutil Module

File, folder, and compressed Package Processing Module

1. shutil. copyfileobj (fsrc, fdst [, length])

Copy the file content to another file, which may be part of the content.

F1 = open ("shutil note", "r", encoding = "UTF-8") f2 = open ("NOTE 2", "w", encoding = "UTF-8") shutil. copyfileobj (f1, f2)

  

2. shutil. copyfile (src, dst)

Copy an object

Shutil. copyfile ("NOTE 2", "NOTE 3 ")

  

3. shutil. copymode (src. dst)

Only the src permission is copied to dst, but the content and modification time of the dst file remain unchanged.

The dst file must exist first.

shutil.copymode("/tmp/score.txt","/tmp/score3.txt")

 

4. shutil. copystat (src. dst)

Copy the information of the src file, including the file content, attributes, and modification time, to the dst file without changing the content of the dst file.

The dst file must exist.

shutil.copystat("/tmp/score.txt","/tmp/score2.txt")

 

5. shutil. copy (src, dst)

Copy files and permissions

shutil.copy("/tmp/score.txt","/tmp/score4.txt")

  

6. shutil. copy2 (src, dst)

Copy the src file content, and grant the permission and status information to the dst file.

shutil.copy2("/tmp/score.txt","/tmp/score5.txt")

 

7. shutil. copytree (path, new_path)

Recursively copy files and copy the files and directories in the directory

shutil.copytree("/tmp/a","/tmp/new_a")

  

8. shutil. rmtree (path)

Delete directory

shutil.rmtree("/tmp/new_a")

  

9. shutil. make_archive (base_name, format ,...)

Create a compressed package and return the file path, such as zip and tar.

  • Base_name: the file name of the compressed package. It can also be the path of the compressed package. Only when the file name is used, it is saved to the current directory; otherwise, it is saved to the specified path,

For example, save www => to the current path
For example:/Users/wupeiqi/www => Save to/Users/wupeiqi/

  • Format: Compressed package type, "zip", "tar", "bztar", and "gztar"
  • Root_dir: Path of the folder to be compressed (default current directory)
  • Owner: user. The current user is used by default.
  • Group: group. Default Value: current group.
  • Logger: used to record logs, usually a logging. Logger object.
shutil.make_archive("G:/python/untitled/study5/shutilstudy", 'gztar', root_dir="G:/python/untitled/study5")

  

 

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.