Can I use an opened gzip file with Popen in Python?

。_饼干妹妹 提交于 2019-12-22 05:20:10

问题


I have a little command line tool that reads from stdin. On the command line I would run either...

./foo < bar

or ...

cat bar | ./foo

With a gziped file I can run

zcat bar.gz | ./foo

in Python I can do ...

Popen(["./foo", ], stdin=open('bar'), stdout=PIPE, stderr=PIPE)

but I can't do

import gzip
Popen(["./foo", ], stdin=gzip.open('bar'), stdout=PIPE, stderr=PIPE)

I wind up having to run

p0 = Popen(["zcat", "bar"], stdout=PIPE, stderr=PIPE)
Popen(["./foo", ], stdin=p0.stdout, stdout=PIPE, stderr=PIPE)

Am I doing something wrong? Why can't I use gzip.open('bar') as an stdin arg to Popen?


回答1:


Because the 'stdin' and 'stdout' of the subprocess takes file descriptor (which is a number), which is an operating system resource. This is masked by the fact that if you pass an object, the subprocess module checks whether the object has a 'fileno' attribute and if it has, it will use it.

The 'gzip' object is not something an operating system provides. An open file is, a socket is, a pipe is. Gzip object is an object that provides read() and write() methods but no fileno attribute.

You can look at the communicate() method of subprocess though, you might want to use it.



来源:https://stackoverflow.com/questions/2732811/can-i-use-an-opened-gzip-file-with-popen-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!