Home > Software engineering >  How do I get the Python sys.exit() code from an exe when running it from a Windows batch file?
How do I get the Python sys.exit() code from an exe when running it from a Windows batch file?

Time:08-10

I have a Python program like this:

import sys

exitCode = 2

if(sys.argv.__len__() > 1):
    exitCode = sys.argv[1]

print('exit code is '   str(exitCode))  

sys.exit(exitCode)

I am calling it from a .bat file like this:

@echo off
cd C:\Users\bmq22\projects\Python\ExitCodeTesting\dist
main.exe 10
echo return code is: %ERRORLEVEL%
pause

I was hoping that %ERRORLEVEL% would contain the return code from the exe (10 in this case), but it does not. This is the output from the .bat file:

exit code is 10
10
return code is: 1
Press any key to continue . . .

So I can see my Python return code printed on the screen, but I can't figure out how to capture it for use in the batch file. Any help is appreciated.

CodePudding user response:

Update: I am an idiot. Needed to convert the exit code argument to an int before exiting.

  • Related