I'm investigating an application that runs programs via a YAML file.
I need to write a Python script that exits with the code specified in the command line argument[1].
My attempt (exit_code_argument.py
):
import sys
sys.exit(sys.argv[1])
The idea is for the Python script to set the Windows Command Prompt's ErrorLevel
variable on exit or termination of the script.
I'm always getting ErrorLevel set to zero:
C:\sandboxes\git\utfa>python exit_code_argument.py 1
1
C:\sandboxes\git\utfa>echo %errorlevel%
1
C:\sandboxes\git\utfa>python exit_code_argument.py 27
27
C:\sandboxes\git\utfa>echo %errorlevel%
1
C:\sandboxes\git\utfa>python exit_code_argument.py 3
3
C:\sandboxes\git\utfa>echo %errorlevel%
1
C:\sandboxes\git\utfa>python exit_code_argument.py 0
0
C:\sandboxes\git\utfa>echo %errorlevel%
1
My goal is to run exit_code_argument.py 3
and have the statement echo %errorlevel%
print 3 on the console (same number as the first parameter to exit_code_argument.py
).
What am I doing wrong or how do I fix the Python script?
The problem is that you need to return an integer, and you're returning a string. Try:
sys.exit(int(sys.argv[1]))
and you should achieve happiness.