详解Python_shutil模块
作者:Vera_y 时间:2023-06-24 00:32:19
import shutil
高级的文件,文件夹,压缩包的处理模块,也主要用于文件的拷贝
shutil.copyfileobj(fsrc,fdst[,length]):
将文件的内容拷贝到另一个文件(可以指定length长度进行拷贝)
import shutil
shutil.copyfileobj(open('old.txt','r'),open('new.txt','w'))
shutil.copyfile(src,dst):
拷贝文件
import shutil
shutil.copyfile('f1.log','f2.log')
shutil.copymode(src,dst):
仅拷贝权限,内容、组、用户均不变
import shutil
shutil.copymode('f1.log', 'f2.log')
shutil.copystat(src,dst):
拷贝状态的信息,包括:mode bits,atime,mtime,flags
import shutil
shutil.copystat('f1.log', 'f2.log')
shutil.copy(src,dst):
拷贝文件和权限
import shutil
shutil.copy('f1.log', 'f2.log')
shutil.copy2(src,dst):
拷贝文件和状态信息
import shutil
shutil.copy2('f1.log', 'f2.log')
shutil.copytree(src,det,symlinks=False,ignore=None):
递归的去拷贝文件
import shutil
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
shutil.rmtree(path[,ignore_errors[,onerror]]):
递归的去删除文件
import shutil
shutil.rmtree('folder1')
shutil.move(src,dst):
递归的去移动文件(重命名)
import shutil
shutil.move('folder1', 'folder3')
shutil.make_archive(base_name, format,...):创建压缩包并返回文件路径,例如:zip、tar
base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径(例:Presley=>保存至当前路径,/User/Presley =>保存至/Users/路径下)
format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要压缩的文件夹路径(默认当前目录)
owner: 用户,默认当前用户
group: 组,默认当前组
import shutil
z = shutil.make_archive('presly', 'gztar', root_dir='D:\软件下载')
shutil对压缩包的处理,也可调用zipfile或tarfile模块进行压缩
以上所述是小编给大家介绍的Python_shutil模块详解整合网站的支持!
来源:https://www.cnblogs.com/Vera-y/p/9671535.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
tensorflow对图像进行拼接的例子
Python使用smtp和pop简单收发邮件完整实例
python Pandas之DataFrame索引及选取数据
Python pyinstaller库的安装配置教程分享
![](https://img.aspxhome.com/file/2023/7/95547_0s.png)
php删除路径下的所有文件夹和文件的代码
Web 2.0 框架发布
![](https://img.aspxhome.com/file/UploadPic/20083/25/200832595359766s.jpg)
webpack5搭建一个简易的react脚手架项目实践
![](https://img.aspxhome.com/file/2023/6/136136_0s.jpg)
使用python模块plotdigitizer抠取论文图片中的数据实例详解
![](https://img.aspxhome.com/file/2023/9/127099_0s.png)
SqlServer Mysql数据库修改自增列的值及相应问题的解决方案
![](https://img.aspxhome.com/file/2023/6/88256_0s.png)
详解如何在Apache中运行Python WSGI应用
Python Selenium安装及环境配置的实现
![](https://img.aspxhome.com/file/2023/4/117434_0s.png)
vue项目中less的一些使用小技巧
python爬虫将js转化成json实现示例
![](https://img.aspxhome.com/file/2023/0/80640_0s.png)
Python实现网络自动化eNSP
![](https://img.aspxhome.com/file/2023/0/64300_0s.png)
简单触发器的使用 献给SQL初学者
![](https://img.aspxhome.com/file/2023/4/113574_0s.jpg)
Linux 自动备份oracle数据库详解
sql server使用公用表表达式CTE通过递归方式编写通用函数自动生成连续数字和日期
![](https://img.aspxhome.com/file/2023/1/110951_0s.png)
JS HTML5拖拽上传图片预览
![](https://img.aspxhome.com/file/2023/8/135898_0s.jpg)
详解MySQL数据库--多表查询--内连接,外连接,子查询,相关子查询
![](https://img.aspxhome.com/file/2023/1/128091_0s.png)