1.压缩一个文件夹下的所有文件
#coding=utf8 import os import tarfile __author__ = ‘Administrator‘ def main(): cwd = os.getcwd() tar = tarfile.open(‘test.tar‘,‘w:gz‘) for root ,dir,files in os.walk(cwd): for file in files: fullpath = os.path.join(root,file) tar.add(fullpath) if __name__==‘__main__‘: main()
import tarfile tar = tarfile.open("sample.tar.gz") tar.extractall() tar.close()
import os import tarfile def py_files(members): for tarinfo in members: if os.path.splitext(tarinfo.name)[1] == ".py": yield tarinfo tar = tarfile.open("sample.tar.gz") tar.extractall(members=py_files(tar)) tar.close()
https://docs.python.org/2/library/tarfile.html
python tarfile模块基本使用,码迷,mamicode.com
原文地址:http://blog.csdn.net/jianhong1990/article/details/24713395