katericata
katericata

Reputation: 1058

Cannot Get The Python Return Code In Bat File (Updated)

I need additional clarification about the question here: Call Python From Bat File And Get Return Code. I do not have the reputation to write my comment there so I am posting a new question.

My Batch file looks like this:

@echo off
start H:\PPython_2.7.5.1\App\python H:\PPython_2.7.5.1\App\checker_task.py
@echo %ERRORLEVEL% > H:\PPython_2.7.5.1\App\output.txt
EXIT /B %ERRORLEVEL%

I am calling a python script which is syntactically and semantically correct and it returns either 0 or 1. The problem is that the %ERRORLEVEL% variable is not influenced by the return value of the Python script. One comment of the answer (https://stackoverflow.com/a/1013293/3865171) from the original question says:

"This will NOT work if %ERRORLEVEL% variable already exists. Python will not overwrite it in that case (but does return the correct code - it will just be hidden by the variable!)."

This is not very clear to me and I cannot find what is wrong. What I want is to be able to return 0 or 1 from Python and based on it to exit the Batch file successfully or not.

Upvotes: 1

Views: 2043

Answers (1)

SachaDee
SachaDee

Reputation: 9545

You have to be in the same instance of CMD to get the correct errorlevel. Try without the start :

@echo off
H:\PPython_2.7.5.1\App\python H:\PPython_2.7.5.1\App\checker_task.py
echo %ERRORLEVEL% > H:\PPython_2.7.5.1\App\output.txt

Upvotes: 2

Related Questions