Python中模块之shutil及zipfile&tarfile的功能介绍

                         shutil的功能介绍及其他打包、压缩模块

1. shutil模块的方法

  1. chown

更改指定路径的属组 2. copy

拷贝文件和权限

    方法:shutil.copy(src,dst,*,follow_symlinks=True)
    返回值:str
    #拷贝源文件到目标文件,权限和内容都将拷贝,但是属组和属主不拷贝,如果不指定文件名,则目录不同时则进行复制相同文件名,返回值为目标文件路径
  1. copy2

    拷贝文件和状态

    方法:shutil.copy2(src,dst,*,follow_symlinks=True)
    返回值:str
    #该方法在copy方法的基础上把文件的最后访问和修改时间也拷贝了,当然创建时间是当前的时间。
    
  2. copyfile

    拷贝文件内容

    方法:shutil.copyfile(src,dst,*,follow_symlinks=True)
    返回值:str
    #该方法只拷贝文件内容,如果目录不同必须要指定文件名
    
  3. copyfileobj

    拷贝文件内容字符串

    方法:shutil.copyfileobj(fsrc,fdst,*,length=16384)
    返回值:None
    例如:f1 = open('file1')
         f2 = open('file2','w')
         shutil.copyfileobj(f1,f2)
         f1.close()
         f2.close()
    #该方法是把文件对象作为参数传入进行拷贝文件内容,被写入的文件要有写入的权限。
    
  4. copymode

    拷贝权限

    方法:shutil.copymode(src,dst,*,follow_symlinks=True)
    返回值:None
    #该方法只复制文件的权限,属组属主和文件内容都不拷贝,如果目标文件不存在将报错。
    
  5. copystat

    拷贝权限和状态

    方法:shutil. copystat(src, dst, *, follow_symlinks=True)
    返回值:None
    #该方法只复制文件的权限和状态,相对于copymode多拷贝了一个最后修改时间和最后访问时间。
    
  6. copytree

    递归拷贝

    方法:shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=<function copy2 at 0x0000000001171488>, ignore_dangling_symlinks=False)
    返回值:str
    #递归的拷贝路径,当目标路径存在时,将报错,返回结果为目标路径
    
  7. disk_usage

    指定盘符使用情况

    方法:shutil.disk_usage(path)
    返回值:shutil.usage
    例如:print(shutil.disk_usage('d:'))
     >>> usage(total=45942304768, used=15426473984, free=30515830784)
    #把指定盘符总容量和已使用和剩余大小(按字节算)以元祖的形式返回,当指定路径为该盘符下的目录时,效果一样。
    
  8. getarchiveformats

    压缩类型整理

    方法:shutil.get_archive_formats()
    返回值:list
    例如:print(shutil.get_archive_formats())
     >>> [('bztar', "bzip2'ed tar-file"), ('gztar', "gzip'ed tar-file"), ('tar', 'uncompressed tar file'), ('xztar', "xz'ed tar-file"), ('zip', 'ZIPfile')]
    #该方法把压缩整理成列表,列表中每个元祖的第一个元素为压缩的后缀名,第二个元素为使用的压缩格式。
    
  9. getterminalsize

    获取终端的大小

    方法:shutil.get_terminal_size(fallback=(80,24))
    返回值:os.terminal_size
    例如:print(shutil.get_terminal_size())
     >>> os.terminal_size(columns=140, lines=38)        
    #获取当前终端窗口的宽度和高度
    
  10. getunpackformats

    解压缩类型

    方法:shutil.get_unpack_formats()
    返回值:list
    #该方法和get_archive_formats方法相反,列表中元祖的元素为解压的名称,扩展名和描述信息。
    
  11. ignore_partterns
  12. make_archive

    创建压缩包并返回路径

    方法:shutil.make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None)
    返回值:str
    例如:print(shutil.make_archive('12.txt','gztar'))
     >>> '12.txt.tar.gz'
    #base_name为被压缩的路径,format为压缩格式,一般有'tar','zip'等,具体可以参考get_archive_formats方法,root_dir指定压缩包存放路径,默认当前路径,owner属主,默认当前用户,group属组,默认当前属组,logger用于记录日志,同常为logging.Logger对象。
    
  13. move

    移动文件

    方法:shutil.move(src,dst)
    返回值:str
    #把文件移动到其他路径,当路径一样时则为重命名,文件的所有信息都将被保留,例如:属主、属组、状态及权限等信息。
    
  14. registerarchiveformat
  15. rmtree

    递归删除文件及目录

    方法:shutil.remtree(path,ignore_errors=False,onerror=None)
    返回值:None
    #递归删除路径
    
  16. unpack_archive
  17. unregisterarchiveformat
  18. unregisterunpackformat
  19. which

    获取命令的路径

    方法:shutil.which(cmd,mode=1,path=None)
    返回值:str
    例如:print(shutil.which('ipconfig'))
     >>> 'C:\\windows\\system32\\ipconfig.EXE'
    #该方法用于查找命令的路径。
    

    注:shutil对压缩包的处理是调用zipfile和tarfile两个模块来进行的。

2. zipfile模块

1. 压缩

方法:z = zipfile.ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True)
     z.write(self, filename, arcname=None, compress_type=None)    
例如:
    f = zipfile.ZipFile('file1.zip','w')
    f = write('123.txt')
    f.close()
#可以指定压缩包存放目录,但需要用‘w’模块打开压缩包,然后可以随意添加文件,最后关闭压缩包。被压缩的文件不会被移动。

2. 解压

方法:z = zipfile.ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True)
     z.extract(self, member, path=None, pwd=None)#单个解压
     z.extractall(self, path=None, members=None, pwd=None)#全部解压
例如:
    f = zipfile.ZipFile('file1.zip','r')
    f.extractall()
    f.close()
#解压压缩包时,打开压缩包需要用‘r’读模式,可以指定解压后的路径,一次性全部解压,最后关闭压缩包。

3. tarfile模块

1. 压缩

方法:t = tar.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
     t.add(self, name, arcname=None, recursive=True, exclude=None, *, filter=None)
例如:t = tar.open('file1.tar','w')
     t.add('14.txt')
     t.close()

2. 解压

方法:t = tar.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
     t.extract(self, member, path='', set_attrs=True, *, numeric_owner=False)
     t.extractall(self, path='.', members=None, *, numeric_owner=False)
例如:t = tar.open('file1.tar','r')
     t.extractall()
     t.close()
#该方法与zipfile模块的压缩与解压方法类似。
posted @ 2017-09-30 21:20  Python改变生活  阅读(370)  评论(0编辑  收藏  举报