Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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'提取存档时出错`_Python_Python 3.x_Archive_Tarfile_Gunzip - Fatal编程技术网

Python 使用'tarfile'提取存档时出错`

Python 使用'tarfile'提取存档时出错`,python,python-3.x,archive,tarfile,gunzip,Python,Python 3.x,Archive,Tarfile,Gunzip,尝试使用提取.tar.gz存档文件时出错 以下是相关的代码片段: #`gzip_archive_bytes_content`是gzip存档的内容,格式为“bytes” repo\u sources\u file\u object=io.BytesIO(gzip\u archive\u bytes\u content) repo\u sources\u tar\u object=tarfile.tarfile(fileobj=repo\u sources\u file\u object) repo

尝试使用提取
.tar.gz
存档文件时出错

以下是相关的代码片段:

#`gzip_archive_bytes_content`是gzip存档的内容,格式为“bytes”
repo\u sources\u file\u object=io.BytesIO(gzip\u archive\u bytes\u content)
repo\u sources\u tar\u object=tarfile.tarfile(fileobj=repo\u sources\u file\u object)
repo\u sources\u tar\u object.extractall(path=“/tmp/”)
这是我得到的错误:

回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.7/tarfile.py”,nti第186行
s=nts(s,“ascii”,“严格”)
文件“/usr/local/lib/python3.7/tarfile.py”,第170行,在nts中
返回s.decode(编码,错误)
UnicodeDecodeError:“ascii”编解码器无法解码位置1中的字节0x9a:序号不在范围内(128)
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.7/tarfile.py”,下一页第2289行
tarinfo=self.tarinfo.fromtarfile(self)
文件“/usr/local/lib/python3.7/tarfile.py”,第1095行,在fromtarfile中
obj=cls.frombuf(buf,tarfile.encoding,tarfile.errors)
文件“/usr/local/lib/python3.7/tarfile.py”,第1037行,在frombuf中
chksum=nti(buf[148:156])
文件“/usr/local/lib/python3.7/tarfile.py”,nti第189行
升起无效标题错误(“无效标题”)
tarfile.InvalidHeaderError:头无效
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.7/runpy.py”,第193行,在“运行”模块中作为“主”
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件“/usr/local/lib/python3.7/runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“/usr/local/lib/python3.7/site-packages/my-packages/\uuuuuuu-main\uuuuuuuuuuuu.py”,第87行,在
函数(**函数参数)
文件“/usr/local/lib/python3.7/site-packages/my-package/chart.py”,第107行,重新安装
安装()
文件“/usr/local/lib/python3.7/site-packages/my-package/chart.py”,第89行,安装中
repo\u sources\u tar\u object=tarfile.tarfile(fileobj=repo\u sources\u file\u object)
文件“/usr/local/lib/python3.7/tarfile.py”,第1484行,在__
self.firstmember=self.next()
文件“/usr/local/lib/python3.7/tarfile.py”,下一页第2301行
raise ReadError(str(e))
tarfile.ReadError:标头无效

Python版本:3.7

我将直接实例化转换为使用,并修复了它:

repo\u sources\u tar\u object=tarfile.open(fileobj=repo\u sources\u file\u object)
文档中实际上有一个警告:

不要直接使用此类:请改用tarfile.open()


我从直接实例化转换为使用,它解决了这个问题:

repo\u sources\u tar\u object=tarfile.open(fileobj=repo\u sources\u file\u object)
文档中实际上有一个警告:

不要直接使用此类:请改用tarfile.open()


最佳做法是使用上下文管理器,以便在作业完成时自动关闭文件

可以这样写:

导入上下文库
输入io
导入tarfile
gzip_归档_字节_内容=b“…”
repo\u sources\u file\u object=io.BytesIO(gzip\u archive\u bytes\u content)
将contextlib.closing(tarfile.open(fileobj=repo\u sources\u file\u object))作为arch:
arch.extractall(path=“/tmp/”)
这在
tarfile.tarfile
中可用,但在
tarfile.open()
中不可用。 所以你可以写:

以tarfile.tarfile(…)作为arch的
:
...

最佳做法是使用上下文管理器,以便在作业完成时自动关闭文件

可以这样写:

导入上下文库
输入io
导入tarfile
gzip_归档_字节_内容=b“…”
repo\u sources\u file\u object=io.BytesIO(gzip\u archive\u bytes\u content)
将contextlib.closing(tarfile.open(fileobj=repo\u sources\u file\u object))作为arch:
arch.extractall(path=“/tmp/”)
这在
tarfile.tarfile
中可用,但在
tarfile.open()
中不可用。 所以你可以写:

以tarfile.tarfile(…)作为arch的
:
...