popen child termination

M

Milos Prudek

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.
 
D

Donn Cave

Quoth Milos Prudek <[email protected]>:
| 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, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top