用Python解压归档时出错

4 投票
2 回答
7488 浏览
提问于 2025-04-17 10:25

我用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

不过我可以在命令行里顺利解压这个文件,没问题。

你们觉得我哪里做错了呢?

2 个回答

0

我会这样做:

import tarfile
target_folder = '.'
with tarfile.open("sample.tar.gz") as tar:
    tar.extractall(target_folder)

就这样。tar / with 会处理剩下的事情。

当你想要获取所有文件的路径时:

import os
filepaths = []
for (dirpath, dirnames, filenames) in walk(target_folder):
    filepaths.extend([os.path.join(dirpath, f) for f in filenames])
17

为了说明,Python的标准库里有一个叫做 tarfile 的模块,它可以自动处理tar、tar.bz2和tar.gz这几种格式。

此外,你还可以做一些很酷的事情,比如获取文件列表、提取特定的文件或文件夹,或者将压缩包分成小块,这样你就可以以流的方式处理它(也就是说,你不需要先解压整个文件再解压,它可以分小块来处理)。

import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()

撰写回答