python压缩与读取.tar.bz2压缩包

例子,python压缩与读取.tar.bz2压缩包。
代码:
 

复制代码代码示例:

#!/bin/python
#
#site: www.jbxue.com 
#压缩文件夹为 .tar.bz2
import tarfile
import bz2
archive = tarfile.open('myarchive.tar.bz2','w:bz2')
archive.debug = 1           # Display the files beeing compressed.
archive.add(r'd:\myfiles')  # d:\myfiles contains the files to compress
archive.close()

#解压一个.tar.bz2
import tarfile
import bz2
archive = tarfile.open('myarchive.tar.bz2','r:bz2')
archive.debug = 1    # Display the files beeing decompressed.
for tarinfo in archive:
    archive.extract(tarinfo, r'd:\mydirectory') # d:\mydirectory is where I want to uncompress the files.
archive.close()

你可能感兴趣的:(Python)