Dave Sellars <da**@didnt.freeserve.co.uk> wrote in
news:40***********************@news.easynet.co.uk:
Is there really no way to run a sub-process, gather its stdout/stderr,
and collect the return-code, on Win32???
But that's what the docs say...
These methods do not make it possible to retrieve the return code
from the child processes. The only way to control the input and
output streams and also retrieve the return codes is to use the
Popen3 and Popen4 classes from the popen2 module; these are only
available on Unix.
Surely not!?!?
Dave.
Bizarrely, the return code is returned as the result of the *last* call to
the close method on any of the file handles returned. i.e. You must close
all of stdin, stdout and stderr handles returned (usually after
reading/writing), and the last one you close will return a numeric exit
code if the command returned a non-zero exit code. If the command returned
a 0 exit code then the final close returns None.
from popen2 import popen2
fout, fin = popen2("dir c:\\temp")
print fout.close(), fin.close()
None None fout, fin = popen2("dir c:\\xxx")
print fout.close(), fin.close()
None 1 fout, fin = popen2("dir c:\\xxx")
print fin.close(), fout.close()
None 1
So far as I can see, the documentation omits to mention this little fact (I
read the source).