Python – 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.


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

Bat <--------------------------------- Python
      * python does a load of work *
      * and returns a return code  *

Best Solution

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


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


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

from sys import exit