shutil模块和os模块对比
来源:互联网 发布:xmind6中文破解版 mac 编辑:程序博客网 时间:2024/06/05 18:01
一、shutil -- 是一种高层次的文件操作工具类似于高级API,而且主要强大之处在于其对文件的复制与删除操作更是比较支持好。
1、shutil.copy(src,dst)复制一个文件到另一个目录下,返回dst路径。dst可以是一个文件,或者是一个目录。但src必须是一个文件,否则会报错。
>>> shutil.copy("e:\\test\\pp.txt","f:\\yy.txt")'f:\\yy.txt'>>> shutil.copy("e:\\test","f:\\")Traceback (most recent call last):File "<pyshell#15>", line 1, in <module>shutil.copy("e:\\test","f:\\")File "D:\Program Files\python3.6\lib\shutil.py", line 235, in copycopyfile(src, dst, follow_symlinks=follow_symlinks)File "D:\Program Files\python3.6\lib\shutil.py", line 114, in copyfilewith open(src, 'rb') as fsrc:PermissionError: [Errno 13] Permission denied: 'e:\\test'
2、shutil.copy2(src,dst)在copy上的基础上再复制文件最后访问时间与修改时间也复制过来了
3、shutil.copyfile(src,dst)从源src复制到dst中去,src和dst必须是文件,不能是文件夹
>>> shutil.copyfile("e:\\test\\pp.txt","f:\\oo.txt")'f:\\oo.txt'>>> shutil.copyfile("e:\\test\\pp.txt","f:\\")Traceback (most recent call last): File "<pyshell#19>", line 1, in <module> shutil.copyfile("e:\\test\\pp.txt","f:\\") File "D:\Program Files\python3.6\lib\shutil.py", line 115, in copyfile with open(dst, 'wb') as fdst:FileNotFoundError: [Errno 2] No such file or directory: 'f:\\'
4、shutil.copystat( src, dst)只复制权限、最后访问时间、最后修改时间,不会复制文件内容
5、shutil.copytree(olddir,newdir,True/Flase)把olddir拷贝一份newdir,如果第3个参数是True,则复制目录时将保持文件夹下的符号连接,如果第3个参数是False,则将在复制的目录下生成物理副本来替代符号连接。
>>> shutil.copytree("e:\\test\\","f:\\yy")'f:\\yy'
6、shutil.copymode(src,dst)只复制权限
7、shutil.move(src,dst)将路径src处的文件夹移动到路径dst,并返回新位置的绝对路径字符串。src可以是一个文件夹,也可以是一个文件。
如果dst目录下已经存在了src中的文件名,则会报错。
>>> shutil.move("e:\\test\\pp.txt","f:\\yy\\uu")'f:\\yy\\uu'>>> shutil.move("e:\\test","f:\\")'f:\\test'>>> shutil.move("e:\\answer1.txt","f:\\yy")Traceback (most recent call last): File "<pyshell#27>", line 1, in <module> shutil.move("e:\\answer1.txt","f:\\yy") File "D:\Program Files\python3.6\lib\shutil.py", line 536, in move raise Error("Destination path '%s' already exists" % real_dst)shutil.Error: Destination path 'f:\yy\answer1.txt' already exists
这里注意一点:如果src中的文件只有只读权限,则会报错,但src文件仍然正常的copy到了dst下。但src不会被删除
>>> shutil.move("e:\\answer1.txt","f:\\yy")Traceback (most recent call last): File "D:\Program Files\python3.6\lib\shutil.py", line 538, in move os.rename(src, real_dst)OSError: [WinError 17] 系统无法将文件移到不同的磁盘驱动器。: 'e:\\answer1.txt' -> 'f:\\yy\\answer1.txt'During handling of the above exception, another exception occurred:Traceback (most recent call last): File "<pyshell#28>", line 1, in <module> shutil.move("e:\\answer1.txt","f:\\yy") File "D:\Program Files\python3.6\lib\shutil.py", line 553, in move os.unlink(src)PermissionError: [WinError 5] 拒绝访问。: 'e:\\answer1.txt'
如果dst所指的目录不存在,而src是一个文件,所以程序默认会认为dst是指的一个没有扩展名的文件,而不是一个文件夹。
>>> shutil.move("e:\\answer2.txt","f:\\qq")'f:\\qq'
这样就在f盘下生成了一个名为qq的文件,,如图所示:
二、os和shutil模块删除文件和文件夹
1、os.unlink(path)将删除path处的文件,成功删除后没有任何返回。path是一个文件的完整路径
os.unlink("f:\\yy\\answer1.txt")
2、os.rmdir(path)将删除path处的文件夹,该文件夹必须为空。其中没有任何文件和文件夹
>>> os.rmdir("f:\\yy\\")Traceback (most recent call last): File "<pyshell#39>", line 1, in <module> os.rmdir("f:\\yy\\")OSError: [WinError 145] 目录不是空的。: 'f:\\yy\\'>>> os.rmdir("f:\\yy\\test")
3、shutil.rmtree(path)将删除path处的文件夹,它包含的所有文件和文件夹都将被删除
- shutil模块和os模块对比
- shutil模块和os模块对比
- os模块和shutil模块
- Python os.path模块和shutil模块
- Python--os模块和shutil模块
- Python--os模块--shutil
- Python的os和shutil模块
- python的 os 和 shutil 模块
- Python中os和shutil模块
- Python模块: 文件和目录os+shutil
- python 文件和文件夹操作 os模块和shutil模块
- python 文件和文件夹操作 os模块和shutil模块
- python 文件操作 os模块和shutil模块
- python 文件操作 os 模块和 shutil 模块
- Python中os和shutil模块实用方法集锦
- Python中os和shutil模块实用方法集锦
- Python中os和shutil模块实用方法集锦
- Python中os和shutil模块实用方法集锦
- Android驱动入门-Led控制+app+ndk库+底层驱动
- 10G 个整数,乱序排列,要求找出中位数,内存限制为 2G
- ML笔记-Introduction
- Git远程仓库(Github)
- MySQL数据库学习
- shutil模块和os模块对比
- 16、ES6 数值和布尔值的解构赋值
- db2修改主机名后配置文件修改
- java中static关键字测试总结
- 【知了堂学习笔记】_JavaScript之DOM操作案例(ATM机)
- AngularJS(基本使用)动态添加表格数据(增删改查)
- JAVA-对称加密算法PBE
- 微信小程序之登录
- CSS初始化