如果没有文件,如何将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