tarfile zipfile bz2 gz
import tarfile import os, shutil filepath = 'd:/python project' ftarname = './test.tar.gz' tarlib = './test/' print(os.getcwd()) #==================gz压缩文件=================== # with tarfile.open(ftarname, mode='w:gz') as tfp: # for file in os.listdir(filepath): # if os.path.isfile(file): # print('compressing file->', file) # tfp.add(file) # #=================获取压缩所有文件名============ # # with tarfile.open(ftarname, mode = 'r:gz') as tfp: # # for name in tfp.getnames(): # # print(name) # # #=================解压所有文件=================== # with tarfile.open(ftarname, mode = 'r:gz') as tfp: # if not os.path.exists(tarlib): # os.mkdir(tarlib) # for mem in tfp.getmembers(): # print('decompressing file->', mem.name) # tfp.extract(mem, tarlib) # 当然也可以用extractall 全部解压 类似的还有bz2 #shutil.rmtree(tarlib) # 清空临时目录