grabbing return codes from os.system() call

David Bolen db3l at fitlinxx.com
Thu Mar 8 22:26:15 EST 2001


"Mark Hadfield" <m.hadfield at niwa.cri.nz> writes:

> "Gregory Jorgensen" <gregj at pobox.com> wrote in message
> news:x8Gp6.899$54.943 at www.newsranger.com...
> > os.system returns the exit status. ... on Windows the
> > exit status is always 0.
> 
> Not so.
> 
> On my Win 2000 box with Python 2.0, successful commands return 0 and
> unsuccessful commands (where the command is not found) return 1.
> 
> Of course there may be more to it, but that's what I found in 20 s of
> experimentation...

The problem is that in order to properly return an exit code of the
child process, the shell in the middle has to pass that information on
up.  This is because os.system() will actually start up a child shell
(e.g., something like "/bin/sh" under Unix or typically "command.com"
or "cmd.exe" under Windows), so the actual command being executed is
one layer removed from the Python script.

This is much more reliable under Unix than in my experience under
various command interpreters under various Windows versions, although
NT (I don't yet use 2K consistently) has been reasonably solid.

I've had decent luck with Windows NT and 2K command interpreters
passing up result codes (and the interpreter will generally return 1
if it can't find the command to execute), but recall problems in old
times with Windows 95 command.com, so if that's what the original
poster had, it may account for the consistent 0 return.

It can also be impacted if the command you are going to run is 16-bit,
since that involves other machinery on each Windows version that often
loses the result code along the way.  All in all a pain, and best
tested in the environment in which its needed.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list