如何在Python中应用shutil模块

发布时间:2021-05-11 16:04:25 作者:Leah
来源:亿速云 阅读:106

今天就跟大家聊聊有关如何在Python中应用shutil模块,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Python主要用来做什么

Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。

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模块有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. Python 之 shutil模块使用
  2. python内置模块~shutil

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python shutil

上一篇:Python中__all__的作用是什么

下一篇:如何在Python中使用Excel操作模块

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》