python中shutil库的使用

python中shutil库的使用

shutil.copyfileobj(fsrc, fdst[, length])
将fsrc文件对象复制到fdst文件对象,其中length如果为负数,表示不按块进行文件的拷贝,如果是整数,则代表拷贝时缓冲区块的大小。copyfileobj要求使用前,fsrc和fdst两个文件对象已经创建。

import shutil
fsrc=open('C:\\Users\\Sun Strong\\Desktop\\hello.txt', 'r+',  encoding='utf-8')
fdst=open('C:\\Users\\Sun Strong\\Desktop\\new_file.txt','w',encoding='utf-8')
shutil.copyfileobj(fsrc, fdst, 20)

shutil.copyfile(src, dst, , follow_symlinks=True)
将文件路径为src的文件拷贝为文件路径为dst的文件,并返回拷贝后得到的文件名(即dst),其中dst必须为完整的文件路径名,如果src与dst相等,则会触发 SameFileError。如果dst拷贝前已存在,则会覆盖。

import shutil,os
os.chdir('C:\\Users\\Sun Strong\\Desktop')
shutil.copyfile('hello.txt', 'new_file.txt')

shutil.copymode(src, dst, , follow_symlinks=True)
将文件路径为src的文件的访问权限信息复制到文件路径为dst的文件中,其中文件内容,所有者,共享组不改变。
首先创建名为“hello.txt”的文件,修改其访问权限为只读。
如下的两个文件’content_copy.txt’以及’permission_copy.txt’区别在于是否进行了shutil.copymode的操作。

import shutil,os
os.chdir('C:\\Users\\Sun Strong\\Desktop')
shutil.copyfile('hello.txt', 'content_copy.txt')
shutil.copyfile('hello.txt','permission_copy.txt')
shutil.copymode('hello.txt','permission_copy.txt')

结果如下
在这里插入图片描述
shutil.copystat(src, dst, , follow_symlinks=True)
将文件路径为src的文件的文件内容,访问权限,最后一次访问时间,最后修改时间以及标记拷贝到文件名为dst的文件中。
现将shutil.copystat与(shutil.copyfile、shutil.copymode)进行对比,

import shutil,os
os.chdir('C:\\Users\\Sun Strong\\Desktop')
shutil.copyfile('hello.txt','permission_copy.txt')
shutil.copyfile('hello.txt','PermissionAndTime_copy.txt')
shutil.copymode('hello.txt','permission_copy.txt')
shutil.copystat('hello.txt','PermissionAndTime_copy.txt')

结果如下:shutil.copystat的操作比shutil.copymode多了访问时间和修改时间的复制。
在这里插入图片描述
shutil.copy(src, dst, , follow_symlinks=True)
shutil.copy等同于先进行了shutil.copyfile操作,然后再进行了shutil.copymode操作,完成操作后的文件内容和访问权限与源文件相同。
shutil.copy2(src, dst, *, follow_symlinks=True)
shutil.copy2就是要将文件路径为src的文件的所有信息尽可能地复制到新文件dst中。

import shutil,os
os.chdir('C:\\Users\\Sun Strong\\Desktop')
shutil.copyfile('hello.txt','permission_copy.txt')
shutil.copyfile('hello.txt','PermissionAndTime_copy.txt')
shutil.copymode('hello.txt','permission_copy.txt')
shutil.copystat('hello.txt','PermissionAndTime_copy.txt')
shutil.copy('hello.txt', 'copyed.txt')
shutil.copy2('hello.txt', 'copy2ed.txt')

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)
递归地拷贝以 src 为根路径的整个目录树,返回目标目录。 名为 dst 的目标目录不必已存在;它本身和还不存在的父目录都将被自动创建。 目录的权限和时间信息将通过 copystat() 来拷贝,单独的文件将使用 shutil.copy2() 来拷贝。其中ingore参数用来排除需要复制的那部分文件。

import shutil,os
from pip._vendor.distlib._backport.shutil import ignore_patterns
path=r"C:\Users\Sun Strong\Desktop"
os.chdir(path)
shutil.copytree('hello', 'new_hello',symlinks=True,ignore=ignore_patterns('*.py'))

本例中,将不会复制目录下的.py文件
shutil.move(src, dst, copy_function=copy2)
递归地将一个文件或目录 (src) 移至另一位置 (dst) 并返回目标位置。如果目标是已存在的目录,则 src 会被移至该目录下。 如果目标已存在但不是目录,它可能会被覆盖,如果目标是在当前文件系统中,则会使用 os.rename()。
shutil.rmtree(path, ignore_errors=False, οnerrοr=None)
删除一个完整的目录树;path 必须指向一个目录(但不能是一个目录的符号链接)。 如果 ignore_errors 为真值,删除失败导致的错误将被忽略;如果为假值或是省略,此类错误将通过调用由 onerror 所指定的处理程序来处理,或者如果此参数被省略则将引发一个异常。
参考文档地址:https://docs.python.org/zh-cn/3.7/library/shutil.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值