Python built-in module (shutil)--034

Source: Internet
Author: User

First, Introduction

Shutil is an advanced file, folder, compression package processing module

Second, use

1. Copy the contents of the file to another file "Shutil.copyfileobj (FSRC, fdst[, length])"

Import shutilf1 = open ("E:\python-project\\f1.txt", ' R ', encoding= ' utf-8 ') F2 = open ("E:\python-project\\f2.txt", ' W ', encoding= ' Utf-8 ') shutil.copyfileobj (F1,F2)

  

2. copy files (copy files) "Shutil.copyfile (SRC, DST)"

Import shutilshutil.copyfile (' E:\python-project\\f1.txt ', ' E:\python-project\\f3.txt ')

  

3, copy only permissions, content, group, users are unchanged "Shutil.copymode (SRC, DST)"

Import Shutilshutil.copymode ("E:\python-project\\f1.txt", ' E:\python-project\\f3.txt ')

  

4. Only copy the status information, including: Mode Bits,atime,mtime,flags "Shutil.copystat (SRC, DST)"

Import Shutilshutil.copystat ("E:\python-project\\f1.txt", ' E:\python-project\\f3.txt ')

5. Copy files and Permissions "Shutil.copy (SRC, DST)"

  

6. Copy files and status information "Shutil.copy2 (SRC, DST)"

Import shutilshutil.copy2 ("E:\python-project\\f1.txt", ' E:\python-project\\f5.txt ')

  

7. Recursive go-to-copy folder "Shutil.copytree (src, DST, Symlinks=false, Ignore=none)"

Import Shutilshutil.copytree ("E:\python-project\Atm", ' E:\python-project\\a ')

  

8. Recursive deletion of Files "Shutil.rmtree (path[, ignore_errors[, onerror])"

Import shutilshutil.rmtree (' E:\python-project\\a ')

  

9, recursive moving files, similar to the MV Command, is actually renamed "Shutil.move (SRC, DST)"

Import shutilshutil.move (' E:\python-project\\f5.txt ', ' E:\python-project\\f6.txt ')

  

10. Processing of compressed packets "shutil.make_archive (base_name, Format,...) 】

Create a compressed package and return the file path, for example: Zip, tar

Base_name: The file name of the compressed package, or the path to the compressed package. Only the file name is saved to the current directory, otherwise saved to the specified path,

For example: www + = Save to Current path

such as:/users/wupeiqi/www = Save to/users/wupeiqi/

Format: Compressed package type, "Zip", "tar", "Bztar", "Gztar"

Root_dir: Folder path to compress (default current directory)

Owner: User, default Current user

Group: Groups, default current group

Logger: Used to log logs, usually logging. Logger Object

Example: Packaging an ATM directory under E:\python-project to Ff.zip in the E:\python-project directory

Import shutilshutil.make_archive (' E:\python-project\\ff ', ' zip ', ' E:\python-project\Atm ')

  

11, Shutil processing of the compression package is done by calling ZipFile and Tarfile two modules.

(1) ZipFile compression decompression (compress the specified file)

Packaged:

Import zipfilez= ZipFile. ZipFile (' E:\python-project\\a.zip ', ' W ') z.write (' E:\python-project\\f6.txt ') z.write (' E:\python-project\\f1.txt ') ) Z.close ()

  

Extract:

z = zipfile. ZipFile (' E:\python-project\\a.zip ', ' R ') Z.extractall () Z.close ()

  

(2) Tarfile compression decompression

Packaged:

Import Tarfiletar = Tarfile.open (' E:\python-project\\b.zip ', ' W ') tar.add (' E:\python-project\\f4.txt ') tar.add (' E:\ Python-project\\f2.txt ') Tar.close ()

  

Extract:

tar = Tarfile.open (' E:\python-project\\b.zip ', ' R ') Tar.extractall () Tar.close ()

  

Python built-in module (shutil)--034

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.