【发布时间】:2019-12-07 00:46:34
【问题描述】:
我正在从 REST API 下载 tarfile,将其写入本地文件,然后在本地提取内容。这是我的代码:
with open ('output.tar.gz', 'wb') as f:
f.write(o._retrieve_data_stream(p).read())
with open ('output.tar.gz', 'rb') as f:
t = tarfile.open(fileobj=f)
t.extractall()
o._retrieve_data_stream(p) 检索文件的数据流。
这段代码运行良好,但对我来说似乎过于复杂。我想我应该能够将字节流直接读取到 tarfile 读取的文件对象中。像这样的:
with open(o._retrieve_data_stream(p).read(), 'rb') as f:
t = tarfile.open(fileobj=f)
t.extractall()
我意识到我的语法可能有点不稳定,但我认为它传达了我正在尝试做的事情。
但是当我这样做时,我得到一个编码错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
发生了什么事?
【问题讨论】:
标签: python networking https tar