import gzip #压缩 g = gzip.GzipFile(filename="", mode=‘wb‘, compresslevel=9, fileobj=open(r‘r:\test.log.gz‘,‘wb‘)) g.write(open(r‘r:\test.log‘).read()) g.close() #解压 g = gzip.GzipFile(mode=’rb’, fileobj=open(r’r:\popopo.gz’,’rb’)) open(r’r:\test.log’,’wb’).write(g.read())
import os import tarfile #压缩,创建tar.gz包 #创建压缩包名 tar = tarfile.open("/tmp/tartest.tar.gz","w:gz") #创建压缩包 for root,dir,files in os.walk("/tmp/tartest"): for file in files: fullpath = os.path.join(root,file) tar.add(fullpath) tar.close() #解压tar.gz包 方法一:解压到指定目录 tar = tarfile.open(tar_path,"r:gz") file_names = tar.getnames() for file_name in file_names: tar.extract(file_name,target_path) tar.close() 方法二: tar = tarfile.open(“tartest.tar.gz”) tar.extract("/tmp") tar.close()
时间: 2024-11-08 06:51:06