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

Posted by eric.frederich on Stack Overflow See other posts from Stack Overflow or by eric.frederich
Published on 2010-04-28T20:15:19Z Indexed on 2010/04/28 20:17 UTC
Read the original article Hit count: 346

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about python

Related posts about subprocess