Shutil module of the Python standard library series
The Shutil module offers a number of high-level operations on files and collections of files. In particular, functions is provided which support file copying and removal. For operations on individual files, see also the OS module.
A module that processes files, folders, and compressed packets.
Official Document: Https://docs.python.org/3.5/library/shutil.html
File and directory Operations
Shutil.copyfileobj (FSRC, fdst[, length])
Copy the contents of a file to another file
>>> import shutil# Loop read Old.txt file contents and write to New.txt file >>> shutil.copyfileobj (open (' Old.txt ', ' R '), open (' New.txt ', ' W ')) >>> import os# View copy past file contents >>> Os.system ("Cat old.txt new.txt") oldold0
Shutil.copyfile (SRC, DST, *, follow_symlinks=true)
Copy the entire file, no second file is created, there is a overwrite
>>> Os.system ("Cat old.txt new.txt") oldnew0>>> shutil.copyfile (' old.txt ', ' new.txt ') # Overwrite the contents of the second file >>> Os.system ("Cat old.txt new.txt") oldold0
Shutil.copymode (SRC, DST, *, follow_symlinks=true)
Copy only file permissions, the contents of the file, the group, the user are not changed
>>> os.system ("Ls -l old.txt new.txt") -rw-rw-rw- 1 ansheng ansheng 4 5 Month 26 15:54 new.txt-rw-rw-r-- 1 ansheng ansheng 4 5 month 26 15:52 old.txt0>>> shutil.copymode (' Old.txt ' , ' new.txt ') # file permissions have changed to 644 >>> os.system ("Ls -l old.txt new.txt") -rw-rw-r-- 1 ansheng ansheng 4 5 Month 26 15:54 New.txt-rw-rw-r-- 1 ansheng ansheng 4 5 month 26 15:52 old.txt0
Shutil.copystat (SRC, DST, *, follow_symlinks=true)
Copy the file status information, the file must exist, do not copy the change time
>>> os.system ("Stat old.txt new.txt") file: ' Old.txt ' Size:4 block:8 IO block:4096 ordinary file equipment:801h/2049d Inode:1835014 Hard Links: 1 permissions: (0664/-rw-rw-r--) uid: ( 1000/ ansheng) gid: ( 1000/ ansheng) recently visited:2016-05-26 15:53:09.813612241 +0800 Recent Changes: 2016-05-26 15:52:54.830640166 +0800 Recent Changes:2016-05-26 15:52:54.830640166 +0800 creation time:- file: ' new.txt ' size:4 Block:8 IO block:4096 ordinary file equipment:801h/2049d inode:1835024 Hard Link: 1 permissions: (0664/-rw-rw-r--) uid: ( 1000/ ansheng) gid: ( 1000 / ansheng) Last visited: 2016-05-26 15:56:22.540041783 +0800 Recent Changes:2016-05-26 15:54:24.244922722 +0800 Recent Changes: 2016-05-26 15:55:38.353967649 +0800 creation time: -0>>> shutil.copystat (' Old.txt ', ' New.txt ') >>> os.system ("Stat old.txt new.txt") file: ' old.txt ' size:4 Block:8 io block:4096 General Document Equipment:801h/2049d inode:1835014 Hard Links: 1 permissions: (0664/-rw-rw-r--) uid: ( 1000/ ansheng) gid: ( 1000/ ansheng) Recent visits: 2016-05-26 15:53:09.813612241 +0800 Recent Changes: 2016-05-26 15:52:54.830640166 + 0800 Recent Changes: 2016-05-26 15:52:54.830640166 +0800 creation time:- file: ' new.txt ' size:4 Block:8 IO block:4096 ordinary file equipment:801h/2049d inode:1835024 Hard Link: 1 permissions: (0664/-rw-rw-r--) uid: ( 1000/ ansheng) gid: ( 1000/ ansheng) last visited: 2016-05-26 15:53:09.813612000 + 0800 last change: 2016-05-26 15:52:54.830640000 +0800 Last modified: 2016-05-26 15:56:48.765143115 + 0800 creation Time: 0
Shutil.copy (SRC, DST, *, follow_symlinks=true)
Copy file and status information, same as copy change time
>>> os.system ("Stat old.txt new.txt") file: ' Old.txt ' Size:4 block:8 IO block:4096 ordinary file equipment:801h/2049d Inode:1835014 Hard Links: 1 permissions: (0664/-rw-rw-r--) uid: ( 1000/ ansheng) gid: ( 1000/ ansheng) recently visited:2016-05-26 15:53:09.813612241 +0800 Recent Changes: 2016-05-26 15:52:54.830640166 +0800 Recent Changes:2016-05-26 15:52:54.830640166 +0800 creation time:- file: ' new.txt ' size:0 Block:0 IO block:4096 Ordinary empty file device:801h/2049d inode:1835023 Hard Link: 1 permissions: (0664/-rw-rw-r--) uid: ( 1000/ ansheng) gid: ( 1000/ ansheng) Recent visits: 2016-05-26 15:57:53.448632439 +0800 Recent Changes: 2016-05-26 15:57:53.448632439 +0800 Recent Changes: 2016-05-26 15:57:53.448632439 +0800 creation time: -0>>> shutil.copy2 (' Old.txt ', ' New.txt ') >>> os.system ("Stat old.txt new.txt") file: ' Old.txt ' size:4 Block:8 io block:4096 Common file Equipment:801h/2049d inode:1835014 Hard Links: 1 permissions: (0664/-rw-rw-r--) uid: ( 1000/ ansheng) gid: ( 1000/ Ansheng) Recent visits: 2016-05-26 15:53:09.813612241 +0800 Recent Changes: 2016-05-26 15:52:54.830640166 + 0800 Recent Changes: 2016-05-26 15:52:54.830640166 +0800 creation time:- file: ' new.txt ' size:4 BLOCK: 8 io Block:4096 ordinary file equipment: 801h/2049d inode:1835023 Hard Links: 1 permissions: (0664/-rw-rw-r--) uid: ( 1000/ ansheng) gid: ( 1000/ ansheng) Recent visit: 2016-05-26 15:53:09.813612000 +0800 Recent Changes:2016-05-26 15:52:54.830640000 +0800 Recent Changes: 2016-05-26 15:58:07.938760974 +0800 creation time: 0
Shutil.ignore_patterns (*patterns)
This factory function creates a function that can is used as a callable for Copytree () ' s ignore argument, ignoring files a nd directories that match one of the glob-style patterns provided. See the example below.
Shutil.copytree (SRC, DST, Symlinks=false, Ignore=none, Copy_function=copy2, Ignore_dangling_symlinks=false)
Recursive go-to-copy folder
>> os.system ("Tree folder1") folder1├── dir├── file.txt├── sc.pyc├── tmp└── vgauthsvclog.txt.0 -> /tmp/vgauthsvclog.txt.02 directories, 3 files0# folder2 directory must not exist, Symlinks=true only copy link file, if equal to false copy source file, ignore equals not copy file or directory >>> Shutil.copytree (' Folder1 ', ' Folder2 ', symlinks=false, ignore=shutil.ignore_patterns (' *.pyc ', ' tmp* ') >>> os.system ("Tree folder2") folder2├── dir├── File.txt└── vgauthsvclog.txt.01 directory, 2 files0
Shutil.rmtree (Path, Ignore_errors=false, Onerror=none)
To delete a file recursively
>>> Os.system ("ls-d folder2") folder20>>> shutil.rmtree (' Folder2 ') >>> os.system ("ls-d Folder2 ") LS: Unable to access ' Folder2 ': no file or directory 512
Shutil.move (SRC, DST, copy_function=copy2)
To move a file recursively, it is like the MV command, which is actually renamed.
>>> Os.system ("Ls-ld folder1") drwxrwxr-x 4 Ansheng ansheng 4096 May 16:09 folder10>>> shutil.move (' F Older1 ', ' Folder3 ') >>> os.system ("ls-ld folder1") LS: Unable to access ' Folder1 ': No file or directory 512>>> os.system ("ls -ld folder3 ") drwxrwxr-x 4 Ansheng ansheng 4096 May 16:09 Folder30
Shutil.make_archive (Base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]] []]]
Create an archive file (such as zip or tar) and return its name.
>>> os.system ("Ls -dl folder3") drwxrwxr-x 4 ansheng ansheng 4096 5 month 26 16:21 folder30# /home/ansheng/folder3 is the saved file, Gztar is the suffix name,/home/ansheng/ Folder3 is the path to be packaged >>> shutil.make_archive ("/home/ansheng/folder3", ' Gztar ', root_dir= '/ Home/ansheng/folder3 ') # return file package put it there '/home/ansheng/folder3.tar.gz ' >>> os.system ("ls -dl /home/ansheng/folder3.tar.gz ")-rw-rw-r-- 1 ansheng ansheng 263 5 month 26 16:22 /home/ansheng/folder3.tar.gz0
The optional parameters are as follows:
Parameters |
Description |
Base_name |
The file name of the compressed package, or the path to the compressed package. |
Format |
Compressed package type, "Zip", "tar", "Bztar", "Gztar" |
Root_dir |
Folder path to compress (default current directory) |
Owner |
User, default Current user |
Group |
Group, default current group |
Shutil processing of the compressed package is done by calling the ZipFile and Tarfile two modules, and the use of these two modules is described later.
#Python标准库 #Shutil
Shutil module of 13Python standard Library series