exit code on windows
db3l at fitlinxx.com
Fri Dec 12 01:47:03 CET 2003
Mark McEahern <marklists at mceahern.com> writes:
> Is there a straightforward way to get the exit code from a windows
It's the result of the close on the handle returned from the os.popen*
calls. For Windows, in the case of the variants returning more than a
single handle, you'll get the exit code when you close the last
handle, and unlike Unix, it's not a "wait"-encoded value but just the
actual result code from the child process.
> I want to write a script that runs a bunch of .sql files against SQL
> Server using osql. I want it to stop if there's an error; e.g.,
> import os
> import glob
> server = 'foo'
> database = 'bar'
> template = 'osql -s%(server)s -d%(database)s -f%(name)s'
> for name in glob.glob('*.sql'):
> cmd = template % locals()
> stdin, stdout = os.popen2(cmd)
> output = stdout.read()
> print stdin.close()
> print stdout.close()
> # On Unix I'd use the return value from the fd.close()
> # to get the exit code, but on Windows the return values
> # here are:
> # None
> # None
The above should work fine, although actually I'm interested in your
Unix comment since I thought under Unix you had to use popen2.Popen3/4
and then use wait() on the resulting objects to get an exit code. But
in any event, it should be fine under Windows (with the final child
process result coming back from the stdout.close() in the above
Are you sure that osql is returning a non-zero result? Zero result
codes are translated to a None return.
A quick test for me seems to work:
stdin, stdout = os.popen2('python rc.py')
output = stdout.read()
> python rctest.py
Now this is with Win2K (or NT/XP). The Win9x series do have a bit of
an issue bubbling up result codes in general, but Python has a helper
module (w9xpopen.exe) that should work around that. One except in
either case can be if you explictly use a top level
command.com/cmd.exe since I think they end up absorbing the exit code
of the command they run.
More information about the Python-list