我使用Python下载了一个bz2文件。然后我想使用以下方法解压缩存档:
def unpack_file(dir, file):
cwd = os.getcwd()
os.chdir(dir)
print "Unpacking file %s" % file
cmd = "tar -jxf %s" % file
print cmd
os.system(cmd)
os.chdir(cwd)
不幸的是,这会以错误结束:
bzip2: Compressed file ends unexpectedly;
perhaps it is corrupted? *Possible* reason follows.
bzip2: Inappropriate ioctl for device
Input file = (stdin), output file = (stdout)
It is possible that the compressed file(s) have become corrupted.
You can use the -tvv option to test integrity of such files.
You can use the `bzip2recover' program to attempt to recover
data from undamaged sections of corrupted files.
tar: Nieoczekiwany EOF w archiwum
tar: Nieoczekiwany EOF w archiwum
tar: Error is not recoverable: exiting now
但是我可以毫无问题地从shell解压缩归档文件。
你有什么想法我做错了吗?
答案 0 :(得分:16)
为了记录,python标准库附带tarfile模块,该模块自动处理tar,tar.bz2和tar.gz格式。
此外,您可以执行诸如获取文件列表,提取文件或目录的子集或者对存档进行分块以使您以流式处理形式处理它(例如,您不必解压缩整个文件然后解压缩)。它以小块的方式完成所有事情)
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()
答案 1 :(得分:0)
我会这样做:
import tarfile
target_folder = '.'
with tarfile.open("sample.tar.gz") as tar:
tar.extractall(target_folder)
如果您想拥有所有文件的路径:
import os
filepaths = []
for (dirpath, dirnames, filenames) in walk(target_folder):
filepaths.extend([os.path.join(dirpath, f) for f in filenames])