压缩模块:tarfile

tarfile与zipfile区别:

1.格式不同

2.压缩模式不同

3,增加,读文件的方法不同

4.extractall不同

导入包:import tarfile

w    单纯的套一个后缀    打包

w:bz2   采用bz2算法    压缩

w:gz   采用gz算法      压缩

压缩大小排序:w》gz》bz2

压缩文件:

add():添加

import tarfile
# res=tarfile.open('D:\\SZC\\ys.tar','w')
# res.add('D:\\SZC\\main.py')
# res.close()
#
# res=tarfile.open('D:\\SZC\\ys.tar.bz2','w:bz2')
# res.add('D:\\SZC\\main.py')
# res.close()
#
# res=tarfile.open('D:\\SZC\\ys.tar.gz','w:gz')
# res.add('D:\\SZC\\main.py')
# res.close()

读取压缩文件的内容:
getnames():读取
# res=tarfile.open('D:\\SZC\\ys.tar.gz','r:gz')
# result=res.getnames()
# print(result)
# res.close()

解压缩文件:
解压单个文件:
# res=tarfile.open('D:/SZC/ys.tar','r')
# res.extract('抽奖.py','D:/SZC/第6章')
# res.close()

解压多个文件:
# res=tarfile.open('D:/SZC/ys.tar','r')
# res.extractall('D:/SZC/第6章')
# res.close()

高级操作:
# with tarfile.open('ys.tar','w')as f:
# f.add('抽奖.py')
# f.add('main.py')
# f.add('100例.py')