What are the commonly used functions in Python?

王林
Release: 2023-05-03 22:13:12
forward
1305 people have browsed it

shutil 是 Python 中的高级文件操作模块,与os模块形成互补的关系,os主要提供了文件或文件夹的新建、删除、查看等方法,还提供了对文件以及目录的路径操作。shutil模块提供了移动、复制、 压缩、解压等操作,恰好与os互补,共同一起使用,基本能完成所有文件的操作。是一个非常重要的模块。

#加载包 import shutil #查看包中的所有方法 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']
Copy after login

01、copy()

描述:复制文件
语法:shutil.copy(fsrc,path),返回值:返回复制之后的路径
fsrc:源文件
path:目标地址

shutil.copy('test.csv','C:/Users/zhengxiang.wzx/Desktop/') 'C:/Users/zhengxiang.wzx/Desktop/test.csv'
Copy after login

02、copy2()

描述:复制文件和状态信息
语法:shutil.copy(fsrc,path),返回值:返回复制之后的路径
fsrc:源文件
path:目标地址

shutil.copy2('test.csv','C:/Users/zhengxiang.wzx/Desktop/') 'C:/Users/zhengxiang.wzx/Desktop/test.csv'
Copy after login

03、copyfileobj()

描述:将一个文件的内容拷贝到另一个文件中,如果目标文件本身就有内容,来源文件的内容会把目标文件的内容覆盖掉。如果文件不存在它会自动创建一个。
语法:shutil.copyfileobj(fsrc, fdst[, length=16*1024])
fsrc:源文件
fdst:复制至fdst文件
length:缓冲区大小,即fsrc每次读取的长度

import shutil f1 = open('file.txt','r') f2 = open('file_copy.txt','w+') shutil.copyfileobj(f1,f2,length=16*1024)
Copy after login
Copy after login

04、copyfile()

描述:将一个文件的内容拷贝到另一个文件中,目标文件无需存在
语法:shutil.copyfile(src, dst,follow_symlinks)
src:源文件路径
dst:复制至dst文件,若dst文件不存在,将会生成一个dst文件;若存在将会被覆盖
follow_symlinks:设置为True时,若src为软连接,则当成文件复制;如果设置为False,复制软连接。默认为True。

import shutil f1 = open('file.txt','r') f2 = open('file_copy.txt','w+') shutil.copyfileobj(f1,f2,length=16*1024)
Copy after login
Copy after login

05、copytree()

描述:复制整个目录文件,不需要的文件类型可以不复制
语法:shutil.copytree(oripath, despath, ignore= shutil.ignore_patterns(".xls", ".doc"))
参数:
oripath : “来源路径”
despath : “目标路径”
ignore : shutil.ignore_patterns() 是对内容进行忽略筛选,将对应的内容进行忽略。

import shutil,os path2 = os.path.join(os.getcwd(),"kaggle") path2 'C:\\Users\\wuzhengxiang\\Desktop\\Python知识点总结\\kaggle' #bbb与ccc文件夹都可以不存在,会自动创建 path3 = os.path.join(os.getcwd(),"bbb","ccc") path3 'C:\\Users\\wuzhengxiang\\Desktop\\Python知识点总结\\bbb\\ccc' # 将"abc.txt","bcd.txt"忽略,不复制 shutil.copytree(path2,path3,ignore=shutil.ignore_patterns("abc.txt","bcd.txt"))
Copy after login

06、copymode()

描述:拷贝权限,前提是目标文件存在,不然会报错。将src文件权限复制至dst文件。文件内容,所有者和组不受影响
语法:shutil.copymode(src,dst)
src:源文件路径
dst:将权限复制至dst文件,dst路径必须是真实的路径,并且文件必须存在,否则将会报文件找不到错误
follow_symlinks:设置为False时,src, dst皆为软连接,可以复制软连接权限,如果设置为True,则当成普通文件复制权限。默认为True。Python3新增参数

shutil.copymode("file_0.csv","file_1.csv")
Copy after login

07、move()

描述:移动文件或文件夹
语法:shutil.move(src, dst)

os.chdir('C:/Users/wuzhengxiang/Desktop/Python知识点总结') os.getcwd() shutil.move('file_1.csv', 'C:/Users/wuzhengxiang/Desktop/股票数据分析') 'C:/Users/wuzhengxiang/Desktop/股票数据分析\\file_1.csv'
Copy after login

08、disk_usage()

描述:查看磁盘使用信息,计算磁盘总存储,已用存储,剩余存储信息。
语法:shutil.disk_usage(‘盘符’)
返回值:元组

shutil.disk_usage('D:') usage(total=151199412224, used=41293144064, free=109906268160) total,总存储:151199412224/1024/1024/1024=140GB used,已使用:41293144064/1024/1024/1024=38GB free,剩余容量:109906268160/1024/1024/1024=102GB
Copy after login

09、 make_archive()

描述:压缩打包
语法:make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,dry_run=0, owner=None, group=None, logger=None)
压缩打包
base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径
format: 压缩或者打包格式 “zip”, “tar”, "bztar"or “gztar”
root_dir : 将哪个目录或者文件打包(也就是源文件)

#把当前目录下的file_1.csv打包压缩 shutil.make_archive('file_1.csv','gztar',root_dir='C:/Users/wuzhengxiang/Desktop/股票数据分析') 'C:\\Users\\wuzhengxiang\\Desktop\\股票数据分析\\file_1.csv.tar.gz'
Copy after login

09、 get_archive_formats()

描述: 获取支持的压缩文件格式。目前支持的有:tar、zip、gztar、bztar。在Python3还多支持一种格式xztar
在学习Python的过程中,往往因为没有资料或者没人指导从而导致自己不想学下去了,因此我特意准备了个群 827513319 ,群里有大量的PDF书籍、教程都给大家免费使用!不管是学习到哪个阶段的小伙伴都可以获取到自己相对应的资料!
语法:unpack_archive(filename, extract_dir=None, format=None)
filename:文件路径
extract_dir:解压至的文件夹路径。文件夹可以不存在,会自动生成
format:解压格式,默认为None,会根据扩展名自动选择解压格式

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)
Copy after login

10、rmtree()

描述:递归的去删除文件
语法:shutil.rmtree(path[, ignore_errors[, onerror]])

#删除文件夹shutil.rmtree('C:/Users/wuzhengxiang/Desktop/Python知识点总结/test2')
Copy after login

The above is the detailed content of What are the commonly used functions in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!