10

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

2
  • how are you planning to do this? Commented Sep 29, 2009 at 10:45
  • 1
    @phoenix24 you should go back to questions you have previously asked and mark the right ones as answered. Your acceptance rating is quite low. 44% Commented Oct 4, 2011 at 14:25

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

IF ERRORLEVEL 1 will match exit codes of >= 1 - support.microsoft.com/kb/69576
4

You can also use python to start your python-program

import subprocess
import sys
retcode = subprocess.call([sys.executable, "myscript.py"])
print retcode

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.