shutil模块和os模块对比

shutil模块和os模块对比

一、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 copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "D:\Program Files\python3.6\lib\shutil.py", line 114, in copyfile
with 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处的文件夹,它包含的所有文件和文件夹都将被删除

s
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值