Run child process with stdout and result on Win32?
Duncan Booth
me at privacy.net
Mon Jun 7 11:01:14 EDT 2004
Dave Sellars <dave at didnt.freeserve.co.uk> wrote in
news:40c47dda$0$17461$afc38c87 at 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).
More information about the Python-list
mailing list