tags:

views:

447

answers:

5

Hi,

I'm running a python program on windowsXP, how can i obtain the exit code after my program ends ?

Thanks.

+4  A: 

From a Windows command line you can use:

echo %ERRORLEVEL%

For example:

C:\work>python helloworld.py
Hello World!

C:\work>echo %ERRORLEVEL%
0
Dave Webb
+1  A: 

How do you run the program?

Exit in python with sys.exit(1)

If you're in CMD or a BAT file you can access the variable %ERRORLEVEL% to obtain the exit code.

For example (batch file):

IF ERRORLEVEL 1 GOTO LABEL
Tarnschaf
IF ERRORLEVEL 1 will match exit codes of >= 1 - http://support.microsoft.com/kb/69576
David Sykes
+1  A: 

You can also use python to start your python-program

import subprocess
import sys
retcode = subprocess.call([sys.executable, "myscript.py"])
print retcode
Blauohr
+1  A: 

If you want to use ERRORLEVEL (as opposed to %ERRORLEVEL%) to check for a specific exit value use

IF ERRORLEVEL <N> IF NOT ERRORLEVEL <N+1> <COMMAND>

For example

IF ERRORLEVEL 3 IF NOT ERRORLEVEL 4 GOTO LABEL
David Sykes
A: 

However I got the following in a script:

set ERRORLEVEL=17
python.exe -c "import sys; sys.exit(1)"
echo %ERRORLEVEL%

==> 17

HOW IS THAT POSSIBLE??

fmuecke