shutil对压缩包的处理是通过调用zipFile和tarFile两个模块来进行的。
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"C:\Users\Administrator\Desktop") z.close()
import tarfile # 压缩 tar = tarfile.open(‘your.tar‘, ‘w‘) tar.add(‘/Users/wupeiqi/PycharmProjects/bbs2.log‘, arcname=‘bbs2.log‘) tar.add(‘/Users/wupeiqi/PycharmProjects/cmdb.log‘, arcname=‘cmdb.log‘) tar.close() # 解压 tar = tarfile.open(‘your.tar‘, ‘r‘) tar.extractall() # 可设置解压地址 tar.close()
时间: 2024-10-12 18:20:28