Python's Popen cleanup

Posted by pythonic metaphor on Stack Overflow See other posts from Stack Overflow or by pythonic metaphor
Published on 2010-04-07T20:14:59Z Indexed on 2010/04/08 0:13 UTC
Read the original article Hit count: 684

Filed under:
|
|

I wanted to use a python equivalent to piping some shell commands in perl. Something like the python version of open(PIPE, "command |").

I go to the subprocess module and try this:

p = subprocess.Popen("zgrep thingiwant largefile", shell=True, stdout=subprocess.PIPE)

This works for reading the output the same way I would in perl, but it doesn't clean itself up. When I exit the interpreter, I get

grep: writing output: Broken pipe

spewed all over stderr a few million times. I guess I had naively hoped all this would be taken care of for me, but that's not true. Calling terminate or kill on p doesn't seem to help. Look at the process table, I see that this kills the /bin/sh process, but leaves the child gzip in place to complain about the broken pipe.

What's the right way to do this?

© Stack Overflow or respective owner

Related posts about popen

Related posts about python