preamble
Python's Shutil module can be seen as a complement to the OS module, which provides methods for copying, moving, deleting, compressing and decompressing files (folders).
The following article will categorize the common methods of the shutil module.
I. Document reproduction
The shutil module provides a variety of file copying methods, the main methods are listed in the following table:
1. (src, dst)
src is the source file path (must be a file) and dst is the target file path (File name may or may not be added)
If the target file exists, it will be overwritten; if it does not exist, a new file will be created, including the path.
Only copy the content and permissions, other file attributes such as time are not copied, note that permissions are copied
The return value is the path to the target file
The code example is as follows:
('../','./') # Copy the file to the current directory, note that dst is the destination path './' ('./') # List filename information for the current directory, created ['test3', '', 'test2', '', '', 'test1'] ('../','./') # Copy the file and rename it, note that dst is the filename './' ('./') # List filename information for the current directory, created ['test3', '', 'test2', '', '', '', 'test1']
2. (src, dst)
src is the source file path (must be a file), dst is the target file path, theand must contain the filename
Destination path does not exist will report an error
The return value is the path to the target file
The code example is as follows:
('../','./') # Copy files with copyfile './' ('../','./os/') # The destination file's directory does not exist will report an error Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.6/", line 121, in copyfile with open(dst, 'wb') as fdst: FileNotFoundError: [Errno 2] No such file or directory: './os/' ('../','./') #dst is not a file, but a directory. Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.6/", line 121, in copyfile with open(dst, 'wb') as fdst: IsADirectoryError: [Errno 21] Is a directory: './'
3. shutil.copy2(src, dst)
- src is the path to the source file and dst is the path to the destination file.
- Copy both the contents of the source file and the stat information (including users, groups, permissions, time information, etc.)
- The return value is the path to the target file
The usage of shutil.copy2() and () is very similar, the difference is that shutil.copy2() not only copies the content of the source file, but also copies the file attributes of the source file, including the modification/access time (the creation time seems not to be able to be copied), permissions and other information. shutil.copy2() is the same as doing () and () at the same time, so that the method name here copy2 can be simply memorized as 2 times copy.
Here is a code example:
('../','./') # Copying files with copy './' (('../').st_mtime) # Return source file modification time 'Sat Jun 8 17:30:32 2019' (('./').st_mtime) # Returns the target file modification time, which is not the same as the source file. 'Sun Sep 18 10:20:36 2022' shutil.copy2('../','./') # Copying files with copy2 './' (('./').st_mtime) # Returns the target file modification time, same as the source file 'Sat Jun 8 17:30:32 2019'
4. (fsrc, fdst,[length=*])
- fsrc is the handle of the source file, fdst is the handle of the target file, length is the size of the copied content.
- The source and target files must be open file handles.
- No return value
() method can copy an open file to another open file, note that the source and target file parameters are file handle objects.
The code example is as follows:
file1=open('','r') # Open the source file file2=open('','w') # Open the target file (file1,file2) #Document copying,Note that there is no return value
5. (src, dst)
- src is the path to the source file, dst is the path to the target file, theCan be a file or a directory
- The target file must already exist, only permission information is copied, not content, time, user and group information.
- The return value is the path to the target file
() will only copy file permissions (st_mode information), not content and other attributes.
The code example is as follows:
('chmod 777 ../') # Use shell commands to change the read and write attributes of the source file 0 ('ls -l ../') # Print the source file with attributes -rwxrwxrwx -rwxrwxrwx 1 user user 527 6moon 8 2019 ../ 0 ('../','./') # Copy files with copyfile './' ('ls -l ./') # Print the target file attribute as -rw-r--r--, not the same as the source file -rw-r--r-- 1 user user 527 9moon 18 10:40 ./ 0 ('../','./')# Copy file permissions with copymode ('ls -l ./') # Print the target file with attributes -rwxrwxrwx, same as the source file -rwxrwxrwx 1 user user 527 9moon 18 10:40 ./ 0
6. (src, dst)
- src is the path to the source file and dst is the path to the destination file.This can be a file or a directory.
- The target file must already exist, only the stat information is copied, including permissions, time, group, user, etc., not the content.
- The return value is the path to the target file
() compared to (), also does not copy the content, the difference is that in addition to copying the permissions information, other stat information including modification time, etc. are also copied by the same.
The code example is as follows:
('../','./') # Copying files './' ('ls -l ../') # Print source file information -rwxrwxrwx 1 user user 527 6moon 8 2019 ../ 0 ('ls -l ./') # Print target file information with different permissions and times than the source file -rw-r--r-- 1 user user 527 9moon 18 10:48 ./ 0 ('../','./') #Copy file stat to target file ('ls -l ./') # Print the target file information with the same permissions and time as the source file -rwxrwxrwx 1 user user 527 6moon 8 2019 ./ 0
7. (src, dst,ignore=none,copy_function=shutil.copy2)
- src is the source path, dst is the destination path, the destination path must not exist
- ignore must be a callable object that () calls ignore once with the current directory and directory's () as arguments each time it recursively accesses a folder, and that must return a sequence of directory and file names relative to the current directory (i.e., a subset of its second argument); these names are subsequently ignored in the copying process. For example, ignore=shutil.ignore_patterns('*.py') means that files or folders ending in .py will not be copied.
- copy_function can choose shutil.copy2 or mode for copy, default is shutil.copy2
- The return value is the path to the target file
() will recursively copy the entire directory tree starting from src to a directory named dst and return the target directory, note that src must be a directory and dst must not already exist. If you need to ignore some files during the copying process, you can use the ignore parameter, note that ignore needs to be followed by a callable object, which can be a customized function with two parameters, the current directory, and the current directory's (), the function returns the name of the file that needs to be ignored. The function can also use shutil comes with shutil.ignore_patterns ('*patterns').
The code example is as follows:
('/home/user/Python','/home/user/Python1',ignore=None,copy_function=) '/home/user/Python1' ('/home/cfzhu/Python1') # List the files in the copied folder. ['', '', '', '.', '.', '', '', '', '', 'shutil', '.', 'os', '', 'file2', '', 'file1', '__pycache__', '', '.', 'file3', '', '', ''] ('/home/user/Python','/home/user/Python2',ignore=shutil.ignore_patterns('*.py'),copy_function=shutil.copy2) '/home/user/Python2' ('/home/user/Python2') #copy files whose names do not end in "*py" ['', '.', '.', 'shutil', '.', 'os', 'file2', 'file1', '__pycache__', '.', 'file3', '']
II. Movement and deletion of documents
1. (src,dst)
- src is the path to the source file and dst is the path to the destination file.Both can be files or directories
- The return value is the path to the target file
The code is as follows (example):
('','') #Moving files '' ('./shutil','./shutil1') #Move the folder './shutil1'
2. (src)
- src is the path to the source file
- No return value
The shutil module rmtree() can recursively delete non-empty folders, use with caution.
The code is as follows:
() ['.bashrc', 'Python2', '.cache', 'Python1', 'hr_py'] ('./Python2/') # Recursively delete all contents of a folder () # Folder deleted [.bashrc', '.cache', 'Python1', 'hr_py']
III. File compression and decompression
1. shutil.make_archive(base_name,format,dir)
- base_name is the path to the source file
- format is the compression format, which can be "zip", "tar", "gztar", "bztar", or "xztar".
- dir is the path to the compressed file
- Returns the path of the compressed file
shutil.make_archive() can compress files or folders, the compression format can be selected, note that compression of the source file does not delete the source file.
The code is as follows (example):
() ['shutil1', '', ''] shutil.make_archive('./shutil1','zip','./') # Compress the file into zip format '/home/cfzhu/Python/'
2. shutil.unpack_archive(filename,extract_dir=None, format=None)
- filename is the path to the source file
- extract_dir is the extracted path, default is the current path.
- format is the compression format, can be "zip", "tar", "gztar", "bztar", or "xztar", do not specify the default is based on the source file extension.
- Returns the path of the compressed file
shutil.unpack_archive() can unpack a compressed file.
The code is as follows (example):
shutil.unpack_archive('./','./shutil2') # Decompression () ['shutil1', '', 'shutil2', '', '', '.', '.', '', ]
summarize
The above is an introduction to the common methods of shutil, including file copying, file moving and deleting and file compression and decompression of three major parts.
Example demonstrations of each method are provided in the text.
to this article on the Python shutil module analysis of the article is introduced to this, more related to Python shutil module content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!