首页 技术随笔

python对压缩包的处理是通过调用zipFile和tarFile两个模块来进行的

技术文档:https://docs.python.org/zh-cn/3/library/zipfile.html

import zipfile

# zipfile压缩
z = zipfile.ZipFile('ss.zip', 'w', zipfile.ZIP_STORED) #打包,zipfile.ZIP_STORED是默认参数
# z = zipfile.ZipFile('ss.zip', 'w', zipfile.ZIP_DEFLATED) #压缩
z.write('ss2')
z.write('ss1')
z.close()

#zipfile解压
z = zipfile.ZipFile('ss.zip', 'r')
z.extractall(path=r"/home/hsm/PycharmProjects/")
z.close()
import tarfile

# 压缩
tar = tarfile.open('your.tar', 'w')
tar.add('/home/hsm/PycharmProjects/bbs2.log', arcname='bbs2.log')
tar.add('/home/hsm/PycharmProjects/cmdb.log', arcname='cmdb.log')
tar.close()

# 解压
tar = tarfile.open('your.tar', 'r')
tar.extractall()  # 可设置解压地址
tar.close()



文章评论