9

I'm running a python program on WindowsXP. How can I obtain the exit code after my program ends?

feetwet
  • 3,248
  • 7
  • 46
  • 84
phoenix24
  • 2,072
  • 2
  • 20
  • 24

4 Answers4

8

From a Windows command line you can use:

echo %ERRORLEVEL%

For example:

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

C:\work>echo %ERRORLEVEL%
0
David Webb
  • 190,537
  • 57
  • 313
  • 299
5

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
  • 3,955
  • 1
  • 26
  • 36
4

You can also use python to start your python-program

import subprocess
import sys
retcode = subprocess.call([sys.executable, "myscript.py"])
print retcode
nosklo
  • 217,122
  • 57
  • 293
  • 297
Blauohr
  • 5,985
  • 2
  • 25
  • 31
2

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
  • 48,469
  • 17
  • 71
  • 80