popen child termination

Donn Cave donn at drizzle.com
Sat Aug 21 23:15:27 EDT 2004


Quoth Milos Prudek <prudek at bvx.cz>:
| How can I make popen (popen2, popen3) run and finish inside my program?
|
| Example:
|
| def Run(cmd):
|      w,r,e = os.popen3(cmd)
|      w.close()
|      r.close()
|      e.close()
| Run('touch ~/xxx')
| os.remove('~/xxx')
|
| The example above fails with this error message for os.remove: "No such 
| file or directory: '~/xxx'"
|
| But when the os.remove line is deleted, the example correctly creates 
| the xxx file.
|
| I feel that os.wait() deals with this but I do not understand how to use 
| it to do what I want.

You're right, but you're going to have to back up a little and
do the popen part a little different.

popen() itself is simpler, because the wait() is built into the 
pclose(3) function that's called by the close() method.  You will
find that it returns exit status, except when exit status is 0 in
which case it returns None.  (Did that make sense?  I hope not.)

As for popen3(), you will need to instead create the Popen3 instance
that popen3 creates.  Then you can invoke its wait() method.  There
isn't any way to get there from just the file objects that popen3()
returns.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list