what happens to Popen()'s parent-side file descriptors?

Seebs usenet-nospam at seebs.net
Thu Oct 14 00:50:22 EDT 2010


On 2010-10-13, Roger Davis <rbd at hawaii.edu> wrote:
> Hi, I am new to this group, please forgive me if this is a repeat
> question. I am a new Python programmer but experienced in C/Unix. I am
> converting a shell script to Python which essentially loops
> infinitely, each pass through the loop running commands like
>
>       set output = `cat this | grep that | whatever ...`
>
> My understanding is that this functionality is best coded via
> subprocess.Popen().

Actually...

It's probably BEST coded, when possible, by doing the work internally.

So:
	thisfile = open('this')
	lines = []
	for line in thisfile:
	    if re.match('that', line):
		lines.append(line)
        whatever(lines)

... or something like that.

(Which you can write as a oneliner list comprehension, but I don't know
that it's got clarity)

Basically, don't use popen() unless the thing you'd be calling is something
hard enough to do that you really, really, want to run an external program
for it.  This is not a shell script; it is not just there to be glue
around other programs.

-s
-- 
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nospam at seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.



More information about the Python-list mailing list