简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM