Python shutil module

Source: Internet
Author: User

Python is a processing module for advanced files, folders, and compression packs

So how does he use it:

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) only copy permissions. Content, groups, and users are unchanged Shutil.copymode (' F1.log ', ' F2.log ') shutil.copystat (SRC, DST) only copy state information, including: mode bits, Atime, Mtime, Flagsshutil.copystat (' F1.log ', ' F2.log ') shutil.copy (src, DST) copy files and Permissions shutil.copy (' F1.log ', ' F2.log ') shutil.copy2 ( SRC, DST) copy files and status information shutil.copy2 (' F1.log ', ' F2.log ') shutil.ignore_patterns (*patterns) shutil.copytree (SRC, DST, Symlinks=false, Ignore=none) recursively 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]]) recursively remove files shutil.rmtree (' Folder1 ') shutil.move (SRC, DST) recursively to move files, which resemble the MV Command, 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 to create the compressed package and return the file path, for example: Zip, Tarbase_name: The file name of the compressed package, or the path of the compressed package. When the file name is just, save movesThe previous directory, otherwise save to the specified path, such as: www + = Save to current path such as:/users/wupeiqi/www = Save to/users/wupeiqi/format: 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 for logging, usually logging. Logger Object #/users/wupeiqi/downloads/test The file under the current program directory Import Shutilret = shutil.make_archive ("wwwwwwwwww", ' Gztar ', Root_dir= '/users/wupeiqi/downloads/test ') #将/users/wupeiqi/downloads/test files under package placement/users/wupeiqi/Catalog 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 # Compress 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 the decompression address 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.