Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python tarfile错误:struct.error:unpack需要长度为4的字符串参数_Python_Tarfile - Fatal编程技术网

python tarfile错误:struct.error:unpack需要长度为4的字符串参数

python tarfile错误:struct.error:unpack需要长度为4的字符串参数,python,tarfile,Python,Tarfile,我有一堆文件,我在这些文件上执行以下处理: for file_name in filelist: tar_file = tarfile.open(file_name) tar_file.extractall("./" + "location") 对于特定文件,我在tarfile.open调用后立即收到此错误: $ file file_name.tgz file_name.tgz: gzip compressed data, from Unix, last modified: Mon

我有一堆文件,我在这些文件上执行以下处理:

for file_name in filelist:
  tar_file = tarfile.open(file_name)
  tar_file.extractall("./" + "location")
对于特定文件,我在
tarfile.open
调用后立即收到此错误:

$ file file_name.tgz 
file_name.tgz: gzip compressed data, from Unix, last modified: Mon Dec 16 16:33:34 2013
回溯(最近一次呼叫最后一次):

文件“”,第1行,在
文件“/usr/lib/python2.7/tarfile.py”,第1660行,打开
返回函数(名称,“r”,文件对象,**kwargs)
文件“/usr/lib/python2.7/tarfile.py”,第1727行,在gzopen中
**kwargs)
taropen中的文件“/usr/lib/python2.7/tarfile.py”,第1705行
返回cls(名称、模式、文件对象、**kwargs)
文件“/usr/lib/python2.7/tarfile.py”,第1574行,在__
self.firstmember=self.next()
文件“/usr/lib/python2.7/tarfile.py”,第2319行,下一页
tarinfo=self.tarinfo.fromtarfile(self)
文件“/usr/lib/python2.7/tarfile.py”,第1239行,在fromtarfile中
buf=tarfile.fileobj.read(块大小)
文件“/usr/lib/python2.7/gzip.py”,第256行,已读
自读(readsize)
文件“/usr/lib/python2.7/gzip.py”,第320行,已读
self._read_eof()
文件“/usr/lib/python2.7/gzip.py”,第339行,在
isize=read32(self.fileobj)#可能超过2GB
read32中的文件“/usr/lib/python2.7/gzip.py”,第25行

返回struct.unpack(“您需要一些异常处理:

for file_name in filelist:
    try:
        tar_file = tarfile.open(file_name)
        tar_file.extractall("./" + "location")
    except struct.error, e:  # or except struct.error as e, depends on Python version
        print "Corrupt:", file_name
    except tarfile.TarError, e:
        print "Tar error (%s): %s" % (str(e), file_name)
这样,您可以看到错误,记录它,但要继续


记录tarfile模块中的异常情况。

有关该文件的详细信息会很有帮助。
file
命令在运行它时会说些什么?
例如,将这个.tar文件归档。
很可能不是tar文件或格式不正确。@SeanPerry添加了信息。您可以使用命令行tar/gz来处理该文件或执行以下操作吗它们也失败了?这看起来像是一个损坏的文件。gzip代码正在遍历该文件,并且在解压缩的一个步骤上失败。@SeanPerry你是对的。这是一个损坏的文件。有没有办法提前检测到并跳过该文件的所有步骤?
for file_name in filelist:
    try:
        tar_file = tarfile.open(file_name)
        tar_file.extractall("./" + "location")
    except struct.error, e:  # or except struct.error as e, depends on Python version
        print "Corrupt:", file_name
    except tarfile.TarError, e:
        print "Tar error (%s): %s" % (str(e), file_name)