Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Python From Bat File And Get Return Code

I'm looking for a way to call a python script from a batch file and getting a return code from the python script. Confusing I know, but it's based on a system that's currently in use. I'd re-write it, but this way would be much, much quicker.

So:

Bat ---------------------> Python
     * call python file *

Bat <--------------------------------- Python
      * python does a load of work *
      * and returns a return code  *
like image 848
Kezzer Avatar asked Jun 18 '09 15:06

Kezzer


People also ask

How do you return a value from a batch file?

Syntax. It is common to use the command EXIT /B %ERRORLEVEL% at the end of the batch file to return the error codes from the batch file. EXIT /B at the end of the batch file will stop execution of a batch file. Use EXIT /B < exitcodes > at the end of the batch file to return custom return codes.

How do I find my return code using CMD?

To display the 0 or 1 return code values, you must type the pdadmin command, followed by either the AIX®, Linux®, or Solaris echo or the Windows errorlevel command: For AIX, Linux, and Solaris operating systems: # pdadmin_command # echo $?


1 Answers

The windows shell saves the return code in the ERRORLEVEL variable:

python somescript.py
echo %ERRORLEVEL%

In the python script you can exit the script and set the return value by calling exit():

exit(15)

In older versions of python you might first have to import the exit() function from the sys module:

from sys import exit
exit(15)
like image 72
sth Avatar answered Oct 01 '22 13:10

sth