1

I am trying to get the exit code from a Batch file. To be more specific I am having issues to get the ERRORLEVEL.

I tried using Popen, check_output,call, check_call:

   out = os.system(BatchFilePath)
   out, err = subprocess.Popen(BatchFilePath,stderr=subprocess.PIPE, shell=True).communicate()
   out,err = subprocess.Popen(BatchFilePath, stderr=subprocess.PIPE).communicate()
   out = subprocess.Popen(BatchFilePath,  shell=True).stderr
   out = os.system(BatchFilePath)
   out = subprocess.check_call(BatchFilePath)
   out = subprocess.call(BatchFilePath, shell=True)
   out = subprocess.check_output(buildPath, shell=True)

Which most of the times return empty or 0

I also tried using

SET ERRORLEVEL=1
exit /B !ERRORLEVEL!

but without luck. I also tried

    set RC=
    setlocal
    somecommand.exe
    endlocal & set RC=%ERRORLEVEL%
    exit /B %RC%

Anotherway to do it is to

out, err = subprocess.Popen(BatchFilePath,stdout=subrocess.PIPE,stderr=subprocess.PIPE, shell=True).communicate()

and searching for the string "ERROR" or "FAILURE" from the out variable.

On the other hand by doing so the user will not see all the echos from the batch file thus the screen will be empty without any messages until the batch file finish and print the appropriate message from my python script.

So I dont need to use the stdout=subrocess.PIPE option from the Popen because its printing all the echos from the batch.

I am working with the CMD and not the powershell. and I am using python 2.7

I searched in the google and here and I wasnt able to find anything that will help me. Any help will be appreciate.

2 Answers 2

1

To get the return code using subprocess.Popen use the poll() or wait() method.

Here an example using poll():

proc = subprocess.Popen('ls')
proc.communicate()
retcode = proc.poll()

Documentation here: https://docs.python.org/2/library/subprocess.html#subprocess.Popen.poll

As per your comment I checked using your batch script

SET ERRORLEVEL=1
exit /B !ERRORLEVEL!

It will work if you to replace ! with %

SET ERRORLEVEL=1
exit /B %ERRORLEVEL%
Sign up to request clarification or add additional context in comments.

4 Comments

I tried that and I still got 0 as return code instead of 1. I made the batch file to fail in purpose.
Your batch file might be the problem. See my edited answer
the only reason I had the '!' instead of '%' was because at the beginning of my batch file i have SETLOCAL enabledelayedexpansion
I tried both ways but still I am getting 0 as Exit code instead of 1
0

The solution to my problem it was really simple but quite nerve-wracking.

I replace the :

EXIT /B !ERRORLEVEL!

with

EXIT 1

and I used the

os.system(BatchFilePath) 

over everything else.

Thank you @olricson, for taking the time to help me. I appreciate that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.