Python中的Untar存档有错误
我使用Python下载了一个bz2文件。然后,我想使用以下方法解压缩存档:Python中的Untar存档有错误,python,Python,我使用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: Comp
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中解压归档文件,没有任何问题
你知道我做错了什么吗?请注意,python标准库附带了自动处理tar、tar.bz2和tar.gz格式的模块 此外,您还可以做一些有趣的事情,如获取文件列表、提取文件或目录的子集或对归档文件进行分块,以便以流形式进行处理(即,您不必解压整个文件,然后将其解压..它以小分块方式完成所有操作)
我会这样做:
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])
您能否向我们展示您在shell中运行的确切命令,以及传递给
os.system()
的确切命令(包括文件名)?请使用而不是os.system
。您如何下载该文件?如果在调用unpack之前进入睡眠状态(15),是否仍然存在相同的错误?谢谢,我不知道tarfile模块。但是我仍然想知道为什么会有错误,'bzcat foo.tar.bz2>foo;echo$?'说什么?tar文件的实际名称是什么?
import os
filepaths = []
for (dirpath, dirnames, filenames) in walk(target_folder):
filepaths.extend([os.path.join(dirpath, f) for f in filenames])