Brief introduction
The Shutil module provides advanced operations for a large number of files. Specifically for file copying and deletion, the main functions are directory and file operations, as well as compression operations. The operation of a single file can also be found in the OS module.
Note Even a higher-level file copy function (Shutil.copy (), shutil.copy2 ()) cannot copy all of the file's metadata. This means that the owner and group of the file and the access control list are lost on the POSIX platform. Resource fork and other metadata cannot be used in Mac OS. This means that the resource will be lost, and the file type and creator code will be incorrect. On windows, file owners, ACLs, and alternate data streams are not replicated.
- Features: Advanced file operations.
- Type: standard module
- Related modules:
- OS standard module.
- ZipFile standard module.
- Tarfile standard module.
Copy files
Shutil.copyfile (SRC, DST): Copies the contents of a file (not including metadata) from SRC to DST. DST must be the full target file name; copy directory see Shutil.copy (). If SRC and DST are the same file, an error shutil will be thrown. Error. DST must be writable, otherwise an exception IOError will be thrown. If DST already exists, it will be replaced. Special files, such as characters or block devices and pipelines, cannot use this feature because CopyFile opens and reads the file. SRC and DST are path names in the form of strings.
*import glob' before: ', Glob(' shutil_copyfile.* ') copyfile(' Shutil_ copyfile.py 'shutil_copyfile.py.copy ')' after: ', Glob(' shutil_copyfile.* ')
Execution Result:
# python Shutil_copyfile.pybefore: [' shutil_copyfile.py ']after: [' shutil_copyfile.py ', ' shutil_copyfile.py.copy ']# Python shutil_copyfile.pybefore: [' shutil_copyfile.py ', ' shutil_copyfile.py.copy ']
CopyFile () calls the bottom function layer copyfileobj ().
Shutil.copyfileobj (FSRC, fdst[, length]): Copies the contents of the file (not including metadata) from the class file object src to the class file to DST. Optional parameter length Specifies the size of the buffer, and a negative number indicates a one-time read-in. By default, the data is cut into small copies to avoid taking up too much memory. Note: The copy is starting from the current file of FSRC.
FromShutilImport*ImportOsFromStringioImport StringioImportSysClassVerbosestringio(Stringio):DefRead(SelfN=-1):Next= Stringio. read(SelfN)Print' Read (%d) bytes '% nReturnNextlorem_ipsum="' Lorem ipsum dolor sit amet, consectetuer adipiscingelit. Vestibulum aliquam mollis dolor. Donec vulputate nunc ut diam. Ut rutrum mi vel sem. vestibulum ante ipsum. "Print' Default: 'Input= Verbosestringio(Lorem_ipsum) output= Stringio() copyfileobj(Input printprint ' all at once: ' input = verbosestringio (Lorem_ipsum) output = stringio () Copyfileobj (input, output -1) print Print ' Blocks of: ' input = verbosestringio ( Lorem_ipsum) output = stringio () Copyfileobj (input256
Execution Result:
# python Shutil_copyfileobj.pyDefault:read (16384) bytesread (16384) Bytesall at Once:read ( -1) bytesread ( -1) bytesblocks of 256:read (bytesread) bytes
Shutil.copy (SRC, DST): Copy file src to file or directory DST. If DST is a directory that is created (or overwritten) with the same file name as SRC, the permission bits are also copied. SRC and DST are path names in the form of strings.
*Osos. mkdir(' example ')' before: ', os. Listdir(' Example ') Copy(' shutil_copy.py 'example ')' after: ', os. Listdir(' Example ' )
Execution Result:
# python Shutil_copy.pybefore: []after: [' shutil_copy.py '
Shutil.copy2 (SRC, DST): Similar to Shutil.copy, metadata is also duplicated, actually calling Shutil.copy first, and then using Copystat. This is similar to the UNIX command cp-p.
FromShutilImport*ImportOsImportTimeDefShow_file_info(filename): Stat_info= OS. stat(filename)Print‘\ tMode: ', Stat_info. St_modePrint‘\ tCreated: ', Time. CTime(Stat_info. st_ctime)Print‘\ tAccessed: '.ctime (Stat_info.st_atime ) print ' \tmodified: ' Time.ctime (Stat_info.st_mtime) Os.mkdir< Span class= "P" > ( ' example ' ) print ' SOURCE: ' Show_ File_info ( ' shutil_copy2.py ' ) Copy2 ( ' shutil_copy2.py ' ' example ' ) Print ' DEST: ' Show_file_info ( ' example/shutil_copy2.py ' ) /span>
Execution Result:
# python Shutil_copy2.pysource: Mode : 33279 created:fri Dec 6 10:45:52 Accessed:fri Dec 6 11:49:40 Modified:mon 01:37:46 2011DEST: Mode : 33279 created:fri Dec 6 11:51:58 Accessed:fri Dec 6 11:49:40 modified:mon Feb 21 01:37:46 2011
Shutil.ignore_patterns (*patterns) is an auxiliary function for Copytree, providing GLOB functionality, example:
Import Copytree, Ignore_patternscopytree(source, Destination, ignore=ignore_patterns (' *.pyc 'tmp* '))
Copy file meta data
When a file is created under UNIX by default based on the Umask setting permission, Copymode () can copy permissions.
Shutil.copymode (SRC, DST): Copy permission bits from SRC to DST. The contents of the file, the owner and the group are unaffected. SRC and DST are path names in the form of strings.
FromShutilImport*FromCommandsImport*ImportOsWithOpen(' File_to_change.txt ', ' wt ') as F: F. Write(' content ') OS. chmod(' file_to_change.txt ') , 0444)print ' before: 'print GetStatus(' file_to_change.txt ') copymode (' shutil_copymode.py ', ' File_to_change.txt ')print ' after: 'print GetStatus (' file_to_change.txt ')
Execution Result:
#./shutil_copymode.py before:-r--r--r--1 root root 7 Dec 7 17:35 file_to_change.txtafter:-rwxrwxrwx 1 root root 7 D EC 7 17:35 File_to_change.txt
To copy a file timestamp, you need to copystat.
Shutil.copystat (SRC, DST): Copy permission bits from SRC, last access time, last modified time, flag to DST. The contents of the file, the owner and the group are unaffected. SRC and DST are given string path names.
FromShutilImport*ImportOsImportTimeDefShow_file_info(filename): Stat_info= OS. stat(filename)Print‘\ tMode: ', Stat_info. St_modePrint‘\ tCreated: ', Time. CTime(Stat_info. st_ctime)Print‘\ tAccessed: ', Time. CTime(Stat_info. st_atime)Print‘\ tModified: ', Time. CTime(Stat_info. st_mtime)WithOpen ( ' file_to_change.txt ' ' wt ' as f: F.write ( "content" ) Os.chmod ( ' file_to_change.txt ' , 0444) print ' before: ' Show_file_info ( ' file_to_change.txt ' ) Copystat ( ' shutil_copystat.py ' ' file_to_change.txt ' ) Span class= "K" >print ' after: ' Show_file_info ( ' File_to_change.txt ' ) /span>
Execution Result:
# python Shutil_copystat.pybefore: Mode : 33060 created:mon Dec 9 10:07:26 Accessed:sat dec< C5/>7 17:35:08 Modified:mon Dec 9 10:07:26 2013AFTER: Mode : 33279 Created:mon Dec 9 10 : 07:26 Accessed:mon Dec 9 10:06:14 Modified:mon Feb 21 01:37:46 2011
Compression decompression
2.7 Later versions provide compression and decompression capabilities.
Shutil.make_archive (Base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]] []]] : Creates an archive file (such as zip or tar) and returns its name. Base_name file name. Format compressed, "Zip", "tar", "Bztar" or "Gztar". Root_dir the root directory of the compression. Base_dir begins the compressed directory. Root_dir and Base_dir are the current directories by default. Owners and groups default to the current user and group; logger is logging. An instance of logger.
Shutil.get_archive_formats (): Returns a list of supported formats. Default support:
- Gztar:gzip Compressed tar file
- Tar files in bztar:bzip2 format
- Tar: Uncompressed tar file
- Zip:zip file
In [3]: Shutil.get_archive_formats () out[3]: [(' Bztar ', "bzip2 ' Ed Tar-file"), (' Gztar ', "gzip Ed Tar-file"), (' Tar ', ' Uncompressed tar file '), (' Zip ', ' zip file ')]
You can add a new format by using Register_archive_format ().
Shutil.register_archive_format (name, function[, extra_args[, description]): Registers a file format. Not used.
Shutil.unregister_archive_format (name): Remove file format, not commonly used.
Compression instance:
from shutil import make_archiveimport span class= "nn" >osarchive_name = os.path.expanduser (Os.path.join ( ' ~ ' ' myarchive ' ) Root_dir = os.path .expanduser (Os.path.join ( ' ~ ' '. SSH ' ) Make_archive ( Archive_name ' Gztar '
The above code generates a file that compresses the. SSH directory in the current user directory.
# tar TZVF myarchive.tar.gz dr-x------root/root 0 2013-10-22 18:52./-RW-------root/root 1675 2013-06-06 20:01. /id_rsa-rw-r--r--root/root 392 2013-06-06 20:01./id_rsa.pub-r--------root/root 1185 2013-08-06 09:41./ Authorized_k
Python-shutil Advanced file Operations