【发布时间】:2020-02-06 15:07:50
【问题描述】:
如果我没有文件,如何将gzip解压的结果传递到进程stdin?
我发现,Popen 构造函数需要stdin 参数才能成为具有fileno 方法的对象。在 python2.7 中 gzip 没有 decompress 功能。还有,为什么Popen 不能接受没有fileno 的文件对象?
我试过这个代码
import subprocess
import gzip
import io
# To test with file
# with open('test.gz', 'rb') as f:
# data = f.read()
data = b'...'
gz = gzip.GzipFile(mode='rb', fileobj=io.BytesIO(data))
subprocess.Popen(['cat'], stdin=gz)
但是有错误:
Traceback (most recent call last):
File "test.py", line 9, in <module>
subprocess.Popen(['cat'], stdin=gz)
File "/usr/lib/python3.7/subprocess.py", line 728, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "/usr/lib/python3.7/subprocess.py", line 1347, in _get_handles
p2cread = stdin.fileno()
File "/usr/lib/python3.7/gzip.py", line 334, in fileno
return self.fileobj.fileno()
io.UnsupportedOperation: fileno
添加:
可以使用不同于Popen 和/或subprocess 的东西
【问题讨论】:
标签: python python-2.7 subprocess gzip