Python's Shutil module

Source: Internet
Author: User

Python's Shutil module

Shutil: Advanced file, folder, compression package processing module

Shutil.copyfileobj (FSRC, fdst[, length]) (Copyfileobj method only copies file contents)
Copy the contents of a file to another file

Import shutilshutil.copyfileobj (' Old.xml ', ' R '), open (' New.xml ', ' W '))

Shutil.copyfile (SRC, DST) (CopyFile Copy only file contents)
Copy files

Shutil.copyfile (' F1.log ', ' F2.log ')

shutil.copy (SRC, DST) copy files and permissions

Shutil.copy (' F1.log ', ' F2.log ')

Shutil.copy2 (SRC, DST)

Copying files and status information

Shutil.copy2 (' F1.log ', ' F2.log '

s Hutil.copymode (SRC, DST) (if the DST file exists, otherwise an error)
Copy permissions only. Content, group, user are not changed

Shutil.copymode (' F1.log ', ' F2.log ')

Shutil.copystat (SRC, DST)
Copy only state information, that is, file attributes, including: Mode bits, Atime, mtime, Flags

Shutil.copystat (' F1.log ', ' F2.log ')

Shutil.ignore_patterns (*patterns) (ignore which file, selective copy)

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

Shutil.copytree (' Folder1 ', ' Folder2 ', ignore=shutil.ignore_patterns (' *.pyc ', ' tmp* '))
Shutil.copytree (' F1 ', ' F2 ', Symlinks=true, Ignore=shutil.ignore_patterns (' *.pyc ', ' tmp* '))

Shutil.rmtree (path[, ignore_errors[, OnError])
To delete a file recursively

Shutil.rmtree (' Folder1 ')

Shutil.move (SRC, DST)
To move a file recursively, it is like the MV command, which is actually renamed.

Shutil.move (' Folder1 ', ' Folder3 ')

Shutil.make_archive (base_name, Format,...)

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

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. When the file name is just, save to the current directory, otherwise save to the specified path, such as: 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
      #将/users/wupeiqi/downloads/test file package to place the current program directory Import Shutilret = shutil.make_archive ("wwwwwwwwww", ' Gztar ', Root_dir = '/users/wupeiqi/downloads/test ') #将/users/wupeiqi/downloads/test file Package Placement/users/wupeiqi/Directory Import Shutilret = Shutil.make_archive ("/users/wupeiqi/wwwwwwwwww", ' Gztar ', root_dir= '/users/wupeiqi/downloads/test ')

Shutil Processing of compressed packets is done by calling the ZipFile and Tarfile two modules, in detail:

Import zipfile# Compression z = zipfile. ZipFile (' Laxi.zip ', ' W ') z.write (' A.log ') z.write (' Data.data ') Z.close () # Unzip z = zipfile. ZipFile (' Laxi.zip ', ' R ') Z.extractall () Z.close ()
Import tarfile# Compressed tar = Tarfile.open (' Your.tar ', ' W ') tar.add ('/users/wupeiqi/pycharmprojects/bbs2.log ', arcname= ' Bbs2.log ') tar.add ('/users/wupeiqi/pycharmprojects/cmdb.log ', arcname= ' Cmdb.log ') Tar.close () # Unzip tar = Tarfile.open ( ' Your.tar ', ' R ') Tar.extractall ()  # can set decompression address tar.close ()


Note: ZipFile compression does not preserve state information for a file, and Tarfile retains state information for the file

Python's Shutil module

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.