Python Standard Library usage example explanation shutil

Turn: https: //www.jb51.net/article/145522.htm

shutil module provides a number of advanced operations on files and collections, in particular, provides support file copy and delete functions.

Folder and file operations

copyfileobj (FSRC, fdst, length = 16 * 1024) : Copy the contents of the file to fdst FSRC file, length FSRC length is read each time, used as the buffer size

  • fsrc: source
  • fdst: Copy the file to fdst
  • length: buffer size, i.e. the length of each read fsrc
?
1
2
3
4
import shutil
f1 = open ( "file.txt" , "r" )
f2 = open ( "file_copy.txt" , "a+" )
shutil.copyfileobj(f1,f2,length = 1024 )

the CopyFile (src, dst) : Copy the contents of the file src to file dst

  • src: source file path
  • dst: dst copied to the file if the file does not exist dst, dst will generate a document; if there will be covered
  • follow_symlinks: set to True, if src is soft connection, as file copy; if set to False, copy soft connection. The default is True. Python3 new parameters
?
1
2
import shutil
shutil.copyfile( "file.txt" , "file_copy.txt" )

CopyMode (src, dst) : Copy the file src to dst file permissions. File contents, owner and group will not be affected

  • src: source file path
  • dst: dst permissions to copy the file, dst path must be true path, and the file must exist, otherwise it will report a file not found error
  • follow_symlinks: When set to False, src, dst are all soft connection, flexible connection permissions can be copied, if set to True, then as a regular file copy rights. The default is True. Python3 new parameters
?
1
2
import shutil
shutil.copymode( "file.txt" , "file_copy.txt" )

copystat (src, dst) : permissions, last access time, last modification time, and a sign copy src to dst. File contents, owner and group will not be affected

  • src: source file path
  • dst: dst permissions to copy the file, dst path must be true path, and the file must exist, otherwise it will report a file not found error
  • follow_symlinks: When set to False, src, dst are all soft connection, flexible connection can be copied permissions, last access time, and time is a sign of src last modified, if set to True, then as a regular file copy rights. The default is True. Python3 new parameters
?
1
2
import shutil
shutil.copystat( "file.txt" , "file_copy.txt" )

Copy (src, dst) : Copy the file src to dst. dst can be a directory, create a file with the same name in the src directory, if the file exists in the directory, an error will be prompted to file the same name already exists. Permissions are copied together. Essentially it has called it copyfile and copymode

  • src: source file path
  • dst: dst copy to a folder or file
  • follow_symlinks: When set to False, src, dst are all soft connection, flexible connection permissions can be copied, if set to True, then as a regular file copy rights. The default is True. Python3 new parameters
?
1
2
3
4
improt shutil,os
shutil.copy( "file.txt" , "file_copy.txt" )
# 或者
shutil.copy( "file.txt" ,os.path.join(os.getcwd(), "copy" ))

copy2 (src, dst) : Copy the file src to dst. dst can be a directory, create a file with the same name in the src directory, if the file exists in the directory, an error will be prompted to file the same name already exists. Permissions, last access time, last modification time and src flag will be copied to dst. Essentially it has called copyfile and copystat method it

  • src: source file path
  • dst: dst copy to a folder or file
  • follow_symlinks: When set to False, src, dst are all soft connection, flexible connection can be copied permissions, last access time, and time is a sign of src last modified, if set to True, then as a regular file copy rights. The default is True. Python3 new parameters
?
1
2
3
4
improt shutil,os
shutil.copy2( "file.txt" , "file_copy.txt" )
# 或者
shutil.copy2( "file.txt" ,os.path.join(os.getcwd(), "copy" ))

ignore_patterns (* Patterns) : override mode for fitting copytree()method, the file transfer will be ignored and will not be copied

  • patterns: the file name, the tuple

copytree (src, dst, symlinks = False, the ignore = None) : a copy of the document tree, the src folder to copy all the contents of the folder dst

  • src: source folder
  • dst: dst Copy to folder, the folder is automatically created, make sure that this folder does not exist, otherwise an error
  • symlinks: whether to copy soft connection, True copy soft connection, False not to copy, soft link to a file will be copied, default False
  • ignore: ignore patterns, can be passedignore_patterns()
  • copy_function: copying files, it can be passed in an executable handler, the default is copy2, new parameters Python3
  • ignore_dangling_symlinks: When sysmlinks set to False, when a copy of the deleted file points to a soft connection, will be given, if you want to eliminate this exception, you can set the value to True. The default is False, Python3 new parameters
?
1
2
3
4
5
6
import shutil,os
folder1 = os.path.join(os.getcwd(), "aaa" )
# bbb与ccc文件夹都可以不存在,会自动创建
folder2 = os.path.join(os.getcwd(), "bbb" , "ccc" )
# 将"abc.txt","bcd.txt"忽略,不复制
shutil.copytree(folder1,folder2,ignore = shutil.ignore_patterns( "abc.txt" , "bcd.txt" )

rmtree (path, ignore_errors = False, onerror = None) : remove the document tree, delete the folder directory

  • ignore_errors: whether to ignore the error, default False
  • the onerror: define the error handling function, a function for an executable transfer processing, the handler receives three parameters: function, path and excinfo
?
1
2
3
import shutil,os
folder1 = os.path.join(os.getcwd(), "aaa" )
shutil.rmtree(folder1)

Move (src, dst) : dst to src is moved to the directory. If dst directory does not exist, the effect is equivalent to the src dst renamed. If the directory exists dst, src will put all the contents of a folder move to the directory

  • src: source folder or file
  • dst: dst to move folders or files will be renamed to dst file. If the folder src, dst file and will report an error
  • copy_function: copies of papers, a handler can pass executable. The default is copy2, Python3 new parameters
?
1
2
3
4
5
6
7
8
9
10
11
12
13
import shutil,os
# 示例一,将src文件夹移动至dst文件夹下面,如果bbb文件夹不存在,则变成了重命名操作
folder1 = os.path.join(os.getcwd(), "aaa" )
folder2 = os.path.join(os.getcwd(), "bbb" )
shutil.move(folder1, folder2)
# 示例二,将src文件移动至dst文件夹下面,如果bbb文件夹不存在,则变成了重命名操作
file1 = os.path.join(os.getcwd(), "aaa.txt" )
folder2 = os.path.join(os.getcwd(), "bbb" )
shutil.move(file1, folder2)
# 示例三,将src文件重命名为dst文件(dst文件存在,将会覆盖)
file1 = os.path.join(os.getcwd(), "aaa.txt" )
file2 = os.path.join(os.getcwd(), "bbb.txt" )
shutil.move(file1, file2)

disk_usage (path) : Gets the current directory where the disk usage. Python3 new method

  • path: the folder or file path. windows must be the path to the folder in linux can be a file path and folder path
?
1
2
3
4
import shutil.os
path = os.path.join(os.getcwd(), "aaa" )
info = shutil.disk_usage(path)
print (info)   # usage(total=95089164288, used=7953104896, free=87136059392)

chown (path, User = None, Group = None) : the owner or modified packet path to a file or folder. Python3 new method

  • path: the path
  • user: the owner, the user must pass the value is true, otherwise no such user error
  • group: grouping, pass the value of the group must be true, otherwise it will report an error no such group
?
1
2
3
import shutil,os
path = os.path.join(os.getcwd(), "file.txt" )
shutil.chown(path,user = "root" ,group = "root" )

Which (cmd, the MODE = os.F_OK | os.X_OK, path = None) : Gets a given cmd command executable file path. Python3 new method

?
1
2
3
import shutil
info = shutil.which( "python3" )
print (info)   # /usr/bin/python3

Archiving

shutil also provides for creating and reading compressed and archived files using the advanced program. Internal implementation relies mainly on zipfile and tarfile module

make_archive (base_name, format, ROOT_DIR, ...) : generate compressed files

  • base_name: the compressed file name, does not allow the extension, as it will generate the appropriate extensions according to the compression format
  • format: Compression format
  • root_dir: will develop compressed folder
?
1
2
3
4
5
6
import shutil,os
base_name = os.path.join(os.getcwd(), "aaa" )
format = "zip"
root_dir = os.path.join(os.getcwd(), "aaa" )
# 将会root_dir文件夹下的内容进行压缩,生成一个aaa.zip文件
shutil.make_archive(base_name, format , root_dir)

get_archive_formats(): 获取支持的压缩文件格式。目前支持的有:tar、zip、gztar、bztar。在Python3还多支持一种格式xztar

unpack_archive(filename, extract_dir=None, format=None): 解压操作。Python3新增方法

  • filename:文件路径
  • extract_dir:解压至的文件夹路径。文件夹可以不存在,会自动生成
  • format:解压格式,默认为None,会根据扩展名自动选择解压格式
?
1
2
3
4
import shutil,os
zip_path = os.path.join(os.getcwd(), "aaa.zip" )
extract_dir = os.path.join(os.getcwd(), "aaa" )
shutil.unpack_archive(zip_path, extract_dir)

get_unpack_formats(): 获取支持的解压文件格式。目前支持的有:tar、zip、gztar、bztar和xztar。Python3新增方法

关于shutil的更多操作:https://docs.python.org/3/library/shutil.html

Guess you like

Origin www.cnblogs.com/kerngeeksund/p/11261834.html