Shutil Copy/move/compress/decompress

Source: Internet
Author: User
Tags disk usage

# shutil_demo.py Advanced file Operations (copy/move/compress/unzip) import Shutil def shutil_demo (): # Copy file Shutil.copy2 (' file.t XT ', ' Temp.txt ') # Copy directory Shutil.copytree ("root", "temp", Symlinks=false, Ignore=shutil.ignore_patterns ("*.pyc")        , Copy_function=shutil.copy2, ignore_dangling_symlinks=true) # Delete directory Shutil.rmtree ("temp", ignore_errors=true) # Move File/directory Shutil.move ("root", "temp", copy_function=shutil.copy2) # Get disk usage space total, used, free = s      Hutil.disk_usage (".") Print ("Current disk total:%IGB, used:%IGB, Remaining:%IGB"% (total/1073741824, used/1073741824, free/1073741824)) # compressed file sh      Util.make_archive (' Box ', ' zip ', ' temp ') # Unzip the file shutil.unpack_archive (' Box.zip ') def shutil_func (): # File and Directory Operations # shutil.copyfileobj (FSRC, fdst[, length])//Copy the contents of the file, copy the contents of the Fsrc file into the Fdst file, length: Buffer size shutil.co Pyfileobj (Open (' file.txt ', ' R '), open (' Temp.txt ', ' W ')) # shutil.copyfile (SRC, DST, *, follow_symlinks=true)//CopyFile contents, with Copyfileobj, if dst=src, throw Samefileerror exception, DST exists replace DST = shutil.copyfile (' file.txt ', ' Temp.txt ') # Shutil. Copymode (SRC, DST, *, follow_symlinks=true)//Copy only permission, other information is not affected Shutil.copymode (' file.txt ', ' Temp.txt ') # Shutil. Copystat (SRC, DST, *, follow_symlinks=true)//Copy status (permissions/Last access time/last modified time/flag), other unaffected by Shutil.copystat (' file.txt ', ' Temp.txt ') # shutil.copy (SRC, DST, *, follow_symlinks=true)//Copy file (data/permissions) DST = shutil.copy (' file.txt ', ' temp . txt ') # shutil.copy2 (SRC, DST, *, follow_symlinks=true)//Copy files (try to keep all metadata) (Cannot copy creation time, this time can be achieved by modifying the system time and then creating files) DST = Shutil.copy2 (' file.txt ', ' Temp.txt ') # shutil.ignore_patterns (*patterns) # symlinks:true (copy link)/False (copy file), Ignore=ignore_patterns ("")//ignored file, copy_function= custom copy function, Ignore_dangling_symlinks:true (ignore file not present exception)/False ( Add exception in error List) # Shutil.copytree (SRC, DST, Symlinks=false, Ignore=none, Copy_function=copy2, Ignore_dangling_symlinks=fal SE)//recursive replication root directory under the entire directory tree DST = ShuTil.copytree ("root", "temp", Symlinks=false, Ignore=shutil.ignore_patterns ("*.pyc"), Copy_function=shutil.copy2, Ignore_dangling_symlinks=true) # Shutil.rmtree (Path, Ignore_errors=false, Onerror=none)//delete entire directory tree, ignore_errors: Whether or not to suddenly Slightly removed failure error, onerror=def error (func, path, Excinfo) shutil.rmtree ("temp", Ignore_errors=true) # shutil.move (SRC, DST       , copy_function=copy2)//recursively move File/directory, directory exists then move directory, file exists overwrite DST = shutil.move ("root", "temp", copy_function=shutil.copy2)  Total, used, free = Shutil.disk_usage (".") # Disk usage statistics for a given path # Shutil.chown (path, User=none, Group=none)//modify user and Group (Unix available) # Shutil.which (cmd, Mode=os. F_OK | Os.  X_OK, Path=none)//path: path to find, unspecified result of using os.environ Path_str = Shutil.which ("python") # exception Try: Pass except Shutil. Samefileerror:pass # CopyFile (), when the source and directory are the same file, this exception is thrown except Shutil.  Error:pass # Copytree () when a multi-file operation throws an exception that contains (SrcName, DstName, excinfo) # Compressed file operation (encapsulated Zipfile/tarfile) # InvasiveBuild archive file Base_name: Compressed package file name, format: Zip/tar/bztar/xztar/gztar, Root_dir: Archived root directory (default current directory) # Base_dir: directory where archive files are saved (default  Current directory) verbose: deprecated dry_run:true (do not create archive, but log), owner: User, Group: User group, Logger: Log # shutil.make_archive (Base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]] [] DST = shutil.make_archive (' Box ', ' Zip ', ' te MP ') # Note: Root_dir/base_dir write at least one, otherwise it will cause the compression package to be packaged again # split archive, filename: filename, Extract_dir: Extract to Directory (default current directory), format: formats (not provided, Based on extension lookup, no throw valueerror found) # shutil.unpack_archive (filename[, extract_dir[, format]) shutil.unpack_archive (' Box.z  IP ') lists = Shutil.get_archive_formats () # Returns the list of supported archive formats [(format, info)] Lists = Shutil.get_unpack_formats () # Returns a list of all registered formats [(name, extensions, description)] # Register compressed format, Name: Format name, function:def func (Base_name, Base_dir, owner, GR OUP, Dry_run, logger), Extra_args: Additional parameters, Description: Description Information # Shutil.register_archive_format (name, function[, Extra_args      [, description]]) # Shutil.unregister_archive_format (name)//Logoff compression format # Register decompression format name: format name, extensions: Extension list, function: Implement Def unpack (filen Ame, Extract_dir), Extra_args: Additional parameters (name, value), Description: Description # Shutil.register_unpack_format (name, extensions, fun ction[, extra_args[, description]) # Shutil.unregister_unpack_format (name)//Logoff Decompression format # terminal # SHUTIL.G Et_terminal_size (fallback= (columns, lines)) columns, lines = shutil.get_terminal_size () # query terminal size (wide, high), cannot query return default size (8   0) If __name__ = = "__main__": Shutil_demo () # Shutil_func ()

  

Shutil Copy/move/compress/decompress

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.