【发布时间】:2017-01-02 12:23:14
【问题描述】:
Create a zip file from a generator in Python? 描述了一种将 .zip 从一堆文件写入磁盘的解决方案。
我在相反的方向也有类似的问题。我得到了一个生成器:
stream = attachment.iter_bytes()
print type(stream)
我很想将它通过管道传输到类似 tar gunzip 文件的对象:
b = io.BytesIO(stream)
f = tarfile.open(mode='r:gz', fileobj = b)
f.list()
但我不能:
<type 'generator'>
Error: 'generator' does not have the buffer interface
我可以像这样在 shell 中解决这个问题:
$ curl --options http://URL | tar zxf - ./path/to/interesting_file
在给定条件下如何在 Python 中做同样的事情?
【问题讨论】:
标签: python python-2.7 generator tar bytesio