How do I check return value of popen2.* ?

Gregory Jorgensen gregj at pobox.com
Sat Mar 10 15:10:32 EST 2001


popen2.popen3 doesn't return the exit status from the child process. From the
documentation:

popen3 (cmd[, bufsize[, mode]]) 
Executes cmd as a sub-process. Returns the file
objects (child_stdout, child_stdin, child_stderr).

(see http://python.org/doc/current/lib/module-popen2.html)

However the popen2.Popen3 class (documented on the same page) does what you
need. This example works for me on a BSD system running Python 1.5.2
---
# t2.py
import sys, popen2

p = popen2.Popen3('./test.py')
p.wait()

# see os.wait() for description of error code
ec = p.poll()
print "exit code: %d, signal %d" % ( (ec >> 8) & 0xFF, ec & 0xFF )
print "---"
print p.fromchild.read()  # dump stdout from child process
print "---"


---
# test.py
import sys

print "hello, world!"
print "bye"
sys.exit(2)
---

Give that a try.

You will find the book "Python Essential Reference" by David Beazley worthwhile.


In article <5vpd89.4ei.ln at 127.0.0.1>, gradha at iname.com says...
>
>Hello.
>
>I have tried the following test. File t2.py:
>
>	#!/usr/bin/env python
>	
>	import sys, popen2
>	
>	sal, en, err = popen2.popen3 ("./test.py")
>	val1 = en.close()
>	print sal.readlines()
>	val2 = sal.close()
>	print err.readlines()
>	val3 = err.close()
>	
>	print val1, val2, val3
>	
>File test.py:
>
>	#!/usr/bin/env python
>	
>	import sys
>	
>	sys.exit(2)
>	
>Now, when I run t2, the output is:
>
>	[]
>	[]
>	None None None
>
>How am I supposed to check the return value of the opened pipe?


Greg Jorgensen
Deschooling Society
Portland, Oregon, USA
gregj at pobox.com



More information about the Python-list mailing list