In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "what are the functions commonly used in the shutil module of Python", the content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn which functions are commonly used in the shutil module of Python.
Shutil is an advanced file operation module in Python, which forms a complementary relationship with os module. Os mainly provides the methods of creating, deleting and viewing files or folders, as well as the path operation of files and directories. The shutil module provides operations such as moving, copying, compression, decompression, etc., which happens to complement and use together with os, and can basically complete the operation of all files. Is a very important module.
# load package import shutil # View all methods in the package print (dir (shutil)) ['chown',' collections', 'copy',' copy2', 'copyfile',' copyfileobj', 'copymode',' copystat', 'copytree',' disk_usage', 'errno',' fnmatch', 'get_archive_formats',' get_terminal_size', 'get_unpack_formats',' getgrnam', 'getpwnam',' ignore_patterns' 'make_archive', 'move',' nt', 'os',' register_archive_format', 'register_unpack_format',' rmtree', 'stat',' sys', 'unpack_archive',' unregister_archive_format', 'unregister_unpack_format',' which']
1. Copy ()
Description: copying fil
Syntax: shutil.copy (fsrc,path). Return value: return the path after replication
Fsrc: source fil
Path: destination addr
Shutil.copy ('test.csv','C:/Users/zhengxiang.wzx/Desktop/')' CJUSERGER UsersUniverse zhengxiang.wzxUniverse Desktopmax test.csv'
2. Copy2 ()
Description: copy files and status information
Syntax: shutil.copy (fsrc,path). Return value: return the path after replication
Fsrc: source fil
Path: destination addr
Shutil.copy2 ('test.csv','C:/Users/zhengxiang.wzx/Desktop/')' CJUSERGER UsersUniverse zhengxiang.wzxUniverse Desktopmax test.csv'
3. Copyfileobj ()
Description: copy the contents of one file to another file, if the target file itself has content, the contents of the source file will overwrite the contents of the target file. If the file does not exist, it will automatically create one.
Syntax: shutil.copyfileobj (fsrc, fdst [, length=16*1024])
Fsrc: source fil
Fdst: copying to fdst fil
Length: buffer size, that is, the length of each read by fsrc
Import shutil F1 = open ('file.txt','r') f2 = open (' file_copy.txt','w+') shutil.copyfileobj
4. Copyfile ()
Description: copy the contents of one file to another file, the target file does not need to exist
Syntax: shutil.copyfile (src, dst,follow_symlinks)
Src: source file path
Dst: copy to the dst file. If the dst file does not exist, a dst file will be generated. If it exists, it will be overwritten.
Follow_symlinks: if set to True, if src is soft connection, it will be copied as file; if set to False, soft connection will be copied. The default is True.
# file_1 does not exist, it will generate a shutil.copyfile ('file_0.csv','file_1.csv')' file_1.csv' # file_2 presence, and directly copy shutil.copyfile ('file_0.csv','file_2.csv')' file_2.csv'
5. Copytree ()
Description: copy the entire directory file, unwanted file types may not be copied
Syntax: shutil.copytree (oripath, despath, ignore= shutil.ignore_patterns ("* .xls", "* .doc"))
Parameters:
Oripath: "Source path"
Despath: "destination path"
Ignore: shutil.ignore_patterns () is to ignore and filter the content and ignore the corresponding content.
Import shutil,os path2 = os.path.join (os.getcwd (), "kaggle") path2'C:\\ Users\\ wuzhengxiang\\ Desktop\\ Python knowledge point summary\\ kaggle' # bbb and ccc folder may not exist, path3 = os.path.join (os.getcwd (), "bbb", "ccc") path3'C:\\ Users\\ wuzhengxiang\ Desktop\ Python knowledge point summary\\ bbb\\ ccc' # will be created automatically "bcd.txt" ignores Do not copy shutil.copytree (path2,path3,ignore=shutil.ignore_patterns ("abc.txt", "bcd.txt"))
6. Copymode ()
Description: copy permissions, provided that the target file exists, otherwise an error will be reported. Copy src file permissions to the dst file. File contents, owners and groups are not affected
Syntax: shutil.copymode (src,dst)
Src: source file path
Dst: copy the permissions to the dst file. The dst path must be a real path and the file must exist, otherwise the file will not be found.
Follow_symlinks: when set to False, both src and dst are soft connections, and you can copy soft connection permissions. If set to True, they are treated as normal file copy permissions. The default is True. New parameters for Python3
Shutil.copymode ("file_0.csv", "file_1.csv")
7. Move ()
Description: move a file or folder
Syntax: shutil.move (src, dst)
Os.chdir ('C:/Users/wuzhengxiang/Desktop/Python knowledge summary') os.getcwd () shutil.move ('file_1.csv',' C:/Users/wuzhengxiang/Desktop/ stock data analysis') 'C:/Users/wuzhengxiang/Desktop/ stock data analysis\\ file_1.csv'
8. Disk_usage ()
Description: view disk usage information, calculate total disk storage, used storage, remaining storage information.
Syntax: shutil.disk_usage ('drive letter')
Return value: tuple
Shutil.disk_usage ('used=41293144064') usage (total=151199412224, used=41293144064, free=109906268160) total, total storage: 151199412224/1024/1024/1024=140GB used, used: 41293144064/1024/1024/1024=38GB free, remaining capacity: 109906268160/1024/1024/1024=102GB
9. Make_archive ()
Description: compressed packaging
Syntax: make_archive (base_name, format, root_dir=None, base_dir=None, verbose=0,dry_run=0, owner=None, group=None, logger=None)
Compressed packaging:
Base_name: the file name of the compressed package, or the path of the compressed package. If it is just a file name, save it to the current directory, otherwise save to the specified path
Format: compressed or packaged format "zip", "tar", "bztar" or "gztar"
Root_dir: which directory or file to package (that is, the source file)
# package the file_1.csv under the current directory and compress shutil.make_archive ('file_1.csv','gztar',root_dir='C:/Users/wuzhengxiang/Desktop/ stock data analysis')'C:\\ Users\\ wuzhengxiang\\ Desktop\\ stock data analysis\\ file_1.csv.tar.gz'
10. Get_archive_formats ()
Description: gets the supported compressed file format. Currently supported are: tar, zip, gztar, bztar. One more format, xztar, is supported in Python3
Syntax: unpack_archive (filename, extract_dir=None, format=None)
Filename: file path
Extract_dir: the folder path to which you extracted it. The folder may not exist and will be generated automatically
Format: decompression format. Default is None, which is automatically selected based on the extension.
Import shutil,os zip_path = os.path.join (os.getcwd (), "file_1.csv.tar") extract_dir = os.path.join (os.getcwd (), "aaa") shutil.unpack_archive (zip_path, extract_dir)
11. Rmtree ()
Description: recursively delete files
Syntax: shutil.rmtree (path [, ignore_errors [, onerror])
# Delete folder shutil.rmtree ('C:/Users/wuzhengxiang/Desktop/Python knowledge summary / test2') Thank you for reading, the above is the content of "what are the functions commonly used in the shutil module of Python". After the study of this article, I believe you have a deeper understanding of what the functions commonly used in the shutil module of Python have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.