20180209-shutil模块

下面讲解shutil模块的相关操作:

  1.shutil.copyfileobj(fsrc, fdst, length=16*1024) 将fsrc文件内容拷贝到fdst文件中,length是指一次拷贝多少个字节

源码:
def
copyfileobj(fsrc, fdst, length=16*1024): """copy data from file-like object fsrc to file-like object fdst""" while 1: buf = fsrc.read(length) if not buf: break fdst.write(buf)

  不难发现,copyfileobj方法中length参数是指一次拷贝多少个字符(以字符形式读取文件,如果以字节的形式读取,则length值字节数),内部的实现通过一个while死循环来遍历fsrc文件的length长度字符,并写入fdst文件中,如果不写则默认length = 16 * 1024

# 1.将文件内容拷贝到另一个文件中 shutil.copyfileobj(fsrc, fdst, length=16*1024),可以指定一次拷贝多少内容,本例不写,则默认读取16 * 1024个字节
ret = shutil.copyfileobj(open('file_01.txt'),open('file_02.txt','w'))
print('将文件内容拷贝到另一个文件中:',ret)

  2.shutil.copyfile(src, dst) 拷贝文件,dst目标文件无需存在

#2.拷贝文件 shutil.copyfile(src, dst) dst目标文件无需存在
ret = shutil.copyfile('file_01.txt','file_03.txt')
print('拷贝文件:',ret)

  3.shutil.copymode(src,dst)仅拷贝权限,内容、组、用户均不变,dst目标文件必须存在

# 3.仅拷贝权限,内容、组、用户均不变 dst目标文件必须存在
ret = shutil.copymode('file_01.txt','file_04.txt')
print('仅拷贝权限:',ret)

  4.shutil.copystat(src,dst)仅拷贝状态的信息,包括mode bits、atime、mtime、flags  dst目标文件必须存在

# 4.仅拷贝文件状态 包括mode bits、atime、mtime、flags  dst目标文件必须存在
ret = shutil.copystat('file_01.txt','file_05.txt')
print('仅拷贝文件状态:',ret)

  5.shutil.copy(src,dst)拷贝文件和权限  dst目标文件无需存在

# 5.shutil.copy(src,dst) 拷贝文件和权限 dst目标文件无需存在
ret = shutil.copy('file_01.txt','file_06.txt')
print('拷贝文件和权限:',ret)

  6.shutil.copy2(src,dst)拷贝文件和状态信息 dst目标文件无需存在

# 6.shutil.copy2(src,dst) 拷贝文件和状态 dst目标文件无需存在
ret= shutil.copy2('file_01.txt','file_07.txt')
print('拷贝文件和状态:',ret)

  7.shutil.copytree(src, dst, symlinks=False, ignore=None) 递归的去拷贝文件夹,ignore参数可以设置忽略文件  dst目标文件不能存在,否则报错

    shutil.ignore_patterns('__init__.py','views.py')

# 7.shutil.copytree(src, dst, symlinks=False, ignore=None) 递归的去拷贝文件夹
ret = shutil.copytree('file01','file02',ignore=shutil.ignore_patterns('__init__.py','views.py'))
print('递归拷贝文件目录:',ret)

  8.shutil.rmtree(src) 递归删除文件 src源文件必须存在,否则报错

# 8.shutil.rmtree(path[, ignore_errors[, onerror]]) 递归的删除文件
ret = shutil.rmtree('file02')
print('递归删除文件:',ret)

  9.shutil.move(src,dst)递归移动文件,src源文件必须存在,否侧报错

# 9.shutil.move(src,dst) 递归移动文件,类似mv命令
ret = shutil.move('file01','file02')
print('递归移动文件:',ret)

重点学习

  项目的目录结构如下:

     

  1.压缩文件
  make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None)
  创建压缩包并返回文件路径,例如zip、tar
  base_name 压缩包的文件名,也可以是压缩包的路径,只是文件名时,则保存到当前目录,否则保存到指定路径
  format 压缩包的种类 'zip'、'tar'、'bztar'、'gztar'
  root_dir 要压缩的文件夹路径(默认当前目录)
  logger 用于记录日志,通常是logging.Logger对象

# 压缩fil02文件夹,由于shutil_exercise.py所在目录是shutil下,如果不指定root_dir,怎会将shutil目录下的所有文件都压缩到file02.zip包中
# 将/file02下文件压缩放置到当前目录下
ret = shutil.make_archive('file02','zip',root_dir='./file02') print('压缩文件:',ret)

  shutil对压缩包的处理是调用ZipFile和TarFile两个模块进行的,详细讲解:

  zipfile的压缩/解压

# 压缩
z = zipfile.ZipFile('file01.zip','w')
z.write('file_01.txt')
z.write('file_02.txt')
z.write('file02')
z.close()

# 解压 
# 将file01.zip压缩包的内容解压到当前目录的file01目录下
z = zipfile.ZipFile('file01.zip','r')
z.extractall(path='./file01')
z.close()

  注意:zipfile不能压缩目录,只能压缩文件,也就意味着压缩后的文件中file02只是一个空目录,目录下的文件不会被压缩

  tarfile的压缩/解压

import tarfile
# 压缩
t = tarfile.open('tmp/file01.tar','w')
t.add('file_01.txt',arcname='file_01.bak')
t.add('file01')
t.close()

# 解压
t = tarfile.TarFile('tmp/file01.tar','r')
t.extractall('tmp/tar')
t.close()

  注意:tarfile可以压缩目录

    

原文地址:https://www.cnblogs.com/it-q/p/8435676.html