I. Introduction
shutil is an advanced file manipulation module in python that complements the os module. os provides methods for creating, deleting, and viewing files and folders, as well as path operations on files and directories. shutil provides operations such as moving, copying, compressing, decompressing, and so on, which complement os and, when used together, basically complete all file operations. operations. It is a very important module.
II. copy()
1. Description: Reproduction of documents
2、Syntax: (fsrc, path), return value: return to the path after copying
- fsrc: source file
- path: destination address
3. Code examples:
import shutil # 1. copy() copies the file result1 = ('/Users/houlei/Desktop/code/python/python_demo/shutil_demo/', '/Users/houlei/Desktop/code/python/python_demo/shutil_demo/') print('result1 = {}'.format(result1))
4. Output results
houlei@houleideMacBook-Pro shutil_demo % python3 shutil_demo.py
result1 = /Users/houlei/Desktop/code/python/python_demo/shutil_demo/
5. Explanation: This is in /Users/houlei/Desktop/code/python/python_demo/shutil_demo directory there is one more
Third, copy2() copy file and status information
1、Description: copy file and state information, the role and usage are similar to copy().
2、Syntax: (fsrc, path), return value: return to the path after copying
- fsrc: source file
- path: destination address
IV. copyfileobj()
1, Description: In the form of a file object, the contents of a file will be copied to another file, if the target file itself has content, the content of the source file will cover the content of the target file. If the file does not exist it will automatically create one.
2. Syntax: (fsrc, fdst[, length=16*1024])
- fsrc: source file
- fdst: copy to fdst file
- length: buffer size, i.e. the length of each read by fsrc
3、Code Demo
f1 = open('', 'r') # Read a file f2 = open('', 'w') # Write to a file (f1, f2, length=10*1024)
4, Description: This will be the contents of the copy to the center of the
V. copyfile()
1、Description: copy the contents of a file to another file, the target file does not need to exist
2. Syntax: (src, dst,follow_symlinks)
- src: path to the source file
- dst: copy to dst file, if dst file does not exist, a dst file will be generated; if it exists, it will be overwritten.
- follow_symlinks: when set to True, if src is a softlink, it will be copied as if it were a file; if set to False, copy the softlink. Default is True
3、Code example
('/Users/houlei/Desktop/code/python/python_demo/shutil_demo/', '/Users/houlei/Desktop/code/python/python_demo/shutil_demo/')
4. Explanation: At this point in the /Users/houlei/Desktop/code/python/python_demo/shutil_demo directory there will be a
VI. copytree()
1、Description: copy the entire directory files, unwanted file types can not be copied
2. Syntax: (oripath, despath, ignore= shutil.ignore_patterns("*.xls", "*.doc"))
- oripath : "source path"
- despath : "Destination path"
- ignore : shutil.ignore_patterns() is an ignore filter on the content, which will ignore the corresponding content.
3、Engineering examples
The project creates a test1, a folder where you can create files with different extensions such as , , etc. for testing.
4、Code Demo
('/Users/houlei/Desktop/code/python/python_demo/shutil_demo/test1', '/Users/houlei/Desktop/code/python/python_demo/shutil_demo/test2', ignore=shutil.ignore_patterns("*.doc")) # Filtering out files with the suffix.docdocuments
VII. move()
1. Description: Move files or folders
2. Syntax: (src, dst)
3、Code Demo
('/Users/houlei/Desktop/code/python/python_demo/shutil_demo/test1/', "/Users/houlei/Desktop/code/python/python_demo/shutil_demo/test2")
viii. disk_usage()
1、Description: View disk usage information, calculate total disk storage, used storage, remaining storage information.
2. Syntax: shutil.disk_usage('disk_usage')
3、Return value: tuple
4、Code Demo
result = shutil.disk_usage("/Users/houlei") print('result = {}'.format(result))
5. Output results
result = usage(total=250790436864, used=155602710528, free=77850771456)
IX. make_archive()
1、Description: compressed packing
2、grammatical:make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,dry_run=0, owner=None, group=None, logger=None)
- base_name: the file name of the archive, or the path of the archive. If it is just a file name, then it will be saved to the current directory, otherwise it will be saved to the specified path.
- format: compression or packing format "zip", "tar", "bztar" or "gztar".
- root_dir : which directory or file to package (a.k.a. the source file)
3、Code Demo
# Package test2 and put it in test1, the compressed name is test2_package, the format is tar. shutil.make_archive(base_name='/Users/houlei/Desktop/code/python/python_demo/shutil_demo/test1/test2_package', format="tar", root_dir='/Users/houlei/Desktop/code/python/python_demo/shutil_demo/test2')
X. get_archive_formats()
1, Description: Get the supported compressed file formats. Currently supported are: tar, zip, gztar, bztar. xztar is also supported in Python3.
2、Code Demo
result = shutil.get_archive_formats() print('result = {}'.format(result))
3. Output results
result = [('bztar', "bzip2'ed tar-file"), ('gztar', "gzip'ed tar-file"), ('tar', 'uncompressed tar file'), ('xztar', "xz'ed tar-file"), ('zip', 'ZIP file')]
xi. unpack_archive()
1. Description: Unzip the file.
2. Syntax: unpack_archive(filename, extract_dir=None, format=None)
- filename: file path
- extract_dir: path of the folder to extract to. The folder may not exist, it will be generated automatically
- format: decompression format, default is None, will automatically select the decompression format according to the extension.
3、Code example
shutil.unpack_archive(filename='/Users/houlei/Desktop/code/python/python_demo/shutil_demo/test1/test2_package.tar', extract_dir='/Users/houlei/Desktop/code/python/python_demo/shutil_demo/test1/test2', format=None)
XII. rmtree()
1、Description: Recursive deletion of files
2. Syntax: (path[, ignore_errors[, onerror]])
3、Code example
('/Users/houlei/Desktop/code/python/python_demo/shutil_demo/test1/test2')
to this article on the python package collection-shutil article is introduced to this, more related python shutil content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!