I. INTRODUCTION
Shutil is an advanced file, folder, and compression package processing module.
Two. Use
Shutil.copyfileobj (FSRC, fdst[, length])
Copy the contents of a file to another file
Import Shutil shutil.copyfileobj (open (' Old.xml ', ' R '), open (' New.xml ', ' W '))
Shutil.copyfile (SRC, DST)
Copy files
Shutil.copyfile (' F1.log ', ' F2.log ')
Shutil.copymode (SRC, DST)
Copy permissions only. Content, group, user are not changed
Shutil.copymode (' F1.log ', ' F2.log ')
Shutil.copystat (SRC, DST)
Copy only the status information, including: mode bits, Atime, mtime, Flags
Shutil.copystat (' F1.log ', ' F2.log ')
Shutil.copy (SRC, DST)
Copying files and Permissions
Shutil.copy (' F1.log ', ' F2.log ')
Shutil.copy2 (SRC, DST)
Copying files and status information
Shutil.copy2 (' F1.log ', ' F2.log ')
Shutil.ignore_patterns (*patterns)
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. 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
#将/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.
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 ()
Python's Shutil module