Python--Shutil module

Source: Internet
Author: User

Shutil Module
Function: File/folder copy, compression processing module
Shutil.copyfileobj (Fsrc,fdst[,length]): Copies the contents of a file to another file, or it can be part of the content
Example: File copy
Import Shutil
# must be ready to read and write files first
sfile = open (' func1.py ', encoding= ' utf-8 ')
Dfile = open (' Func1.py.bak ', ' W ', encoding= ' utf-8 ')
# Make a copy of the content
Shutil.copyfileobj (Sfile,dfile)
Note: Obviously the above method is not practical, the following method is most practical!
Shutil.copy (SRC,DST): Copy source files directly to destination, including file permissions
such as: shutil.copy ('/var/log/message ', ' message ')
Shutil.copyfile (SRC,DST): Copy the source file directly to the destination, excluding file permissions
Shutil.copymode (SRC,DST): Copy file permissions, user group unchanged
Shutil.copystat (SRC,DST): Information about copying file status (attributes such as file time), including: Mode Bits,atime,mtime,flags
Shutil.copy2 (SRC,DST): Copy file and permission state information (full copy)
Shutil.copytree (SRC,DST): Copy Entire Directory
Shutil.rmtree (src): Delete directory
Shutil.move (SRC,DST): Move a file or folder
Shutil.make_archive (Base_name,format,..) : Creates a compressed package and returns the file path. Cons: cannot be filtered. Example: Zip,tar
Base_name: The file name of the compressed package, or it can be the package path, just the file name, save to the current directory, otherwise save to the specified directory.
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: User Logging
Example: Package a/var/log file into the current directory
Import Shutil
ret = shutil.make_archive (' wwwwww ', ' Gztar ', root_dir= '/var/log ')
Example 2: Package/var/log/files into the/root/directory
ret = shutil.make_archive (' root/wwww ', ' Gztar ', root_dir= '/var/log ')
Note: If the above package is zip, it is best to unpack with the ZipFile module.
Shutil handling of compressed packets is to invoke the ZipFile and Tarfile modules
For example: Compression and decompression using ZipFile
Import ZipFile
#注意, ZipFile can only compress files, compressed directories only directory structure
#压缩:
z = zipfile. ZipFile (' Bk.zip ', ' W ')
Z.write (' A.log ')
Z.write (' Data.log ')
Z.close ()
#解压
z = zipfile. ZipFile (' Bk.zip ', ' R ')
Z.extractall ()
Z.close ()
such as: packaging with tar (but not compression, compression requires the use of ZipFile)
Import Tarfile
tar = Tarfile.open ('/root/your.tar ', ' W ')
#注意: Arcname is used here, then only the files under the log are packaged in the test folder, and if you do not have the Arcname parameter, the package is the path + file
Tar.add ('/var/log ', arcname= ' test ')
Tar.add ('/test.zip ')
Tar.close ()

Python--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.