扣丁学堂Python培训之详解shutil模块

2019-03-15 16:13:38 1048浏览

今天扣丁学堂Python培训老师给大家分享一篇关于详解shutil模块,文中通过示例代码介绍的非常详细,希望对同学们学习有所帮助,下面我们一起来看一下吧。



importshutil

高级的文件,文件夹,压缩包的处理模块,也主要用于文件的拷贝

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):拷贝状态的信息,包括:modebits,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:组,默认当前组

importshutil

z=shutil.make_archive('presly','gztar',root_dir='D:\软件下载')

shutil对压缩包的处理,也可调用zipfile或tarfile模块进行压缩

以上就是关于扣丁学堂Python培训之详解shutil模块详细整合介绍,希望对大家有所帮助,

想要学好Python开发小编给大家推荐口碑良好的扣丁学堂,扣丁学堂有专业老师制定的Python学习路线图辅助学员学习,此外还有与时俱进的Python视频教程供大家学习,想要学好Python开发技术的小伙伴快快行动吧。扣丁学堂Python技术交流群:279521237。


扣丁学堂微信公众号 


关注微信公众号获取更多学习资料 

 

查看更多关于"Python开发资讯"的相关文章>

标签: Python培训 Python视频教程 Python在线视频 Python学习视频 Python培训班

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号