[Tutor] Getting exit code in Windows

Tim Peters tutor@python.org
Mon, 01 Apr 2002 01:19:47 -0500


[Victor R. Cardona]
> I am writing a simple shell in python as a small demonstration project.
> I have figured out how to get the exit code for a process on UNIX using
> the waitpid function in the os module. Unfortunately, this function does
> not seem to exist on Windows.

os.waitpid() works under Windows in Python 2.3, but we're a long way from
releasing that.

> Is there a portable way of getting that kind of information? I know I
> could use the win32all extensions, but I wanted to make sure there was
> not a better way.

I'm afraid there really isn't.  os.system('command string') runs a shell
command and returns its exit code, so you'd *think* that would be portable
(at least among systems that *have* "a shell"; for example, Mac Classic does
not).  But it turns out that the way Microsoft implemented the Win9x shell
(command.com), os.system() *always* returns 0 on Win9x.  command.com doesn't
pass along the exit code of the command it ran, it unaccountably and
uselessly returns its own "hey, I ran a command!  that's a success!" 0 exit
code.

So you're stuck.

computers-are-great-ly y'rs  - tim