Why sometimes Python subprocess failed to get the correct exit code after running a process?

回眸只為那壹抹淺笑 提交于 2019-12-30 10:09:36

问题


I am using Python subprocess to run external scripts on Windows 7. I am trying to get the exit code.


In case 1, I run a python script test1.py.

test1.py

import sys
sys.exit(24)   <--exit code

myscript1.py

import subprocess
process = subprocess.Popen(["python", "C:\\path\\to\\test1.py"], stdout=subprocess.PIPE)
process.wait()
print process.returncode

In Windows command prompt, when I run the script, I get the following output:

>python test1.py
>
>echo %errorlevel%
>24
>
>python myscript1.py
>24

So, you can see that subprocess is able to get the correct exit code in this case.


In case 2, I run a batch file test2.cmd.

test2.cmd

EXIT /B 56   <--exit code

myscript2.py

import subprocess
process = subprocess.Popen(["C:\\path\\to\\test2.cmd"], stdout=subprocess.PIPE)
process.wait()
print process.returncode

In Windows command prompt, when I run the script, I get the following output:

>test2.cmd
>
>echo %errorlevel%
>56
>
>python myscript2.py
>56

So, you can see that subprocess is also able to get the correct exit code in this case.


In case 3, I run a SikuliX script.

test3.sikuli

xxx xxx (sikuli script here)
xxx xxx
...
exit(16)   <--exit code

myscript3.py

import subprocess
process = subprocess.Popen(["C:\\path\\to\\runsikuli.cmd", "-r", "C:\\path\\to\\sikuli-script.sikuli"], stdout=subprocess.PIPE)
process.wait()
print process.returncode

In Windows command prompt, when I run the script, I get the following output:

>C:\path\to\runsikuli.cmd -r C:\path\to\sikuli-script.sikuli
>... (stdout + stderr)
>16
>
>echo %errorlevel%
>16
>
>python myscript3.py
>0

In case 3, when I run the script manually in the command prompt, it is able to set the %errorlevel%. When I run the script using Python subprocess, subprocess is unable to get the correct exit code. It always return 0.

Why Python subprocess failed to get the exit code in case 3?


回答1:


As your comment says if you run the same command then both the command prompt and using Python subprocess module produce the same result (0 exit code).

You may see different exit codes because you use different commands. No surprise here. Python subprocess module returns correct (for a given command) exit code.


If it doesn't return the right exit status then you could add & exit at the end of your cmd.exe command as a workaround to get the correct return code, see "subprocess on Windows: wrong return code with shell=True" on Python issue tracker:

from subprocess import check_call

check_call(r'C:\path\to\runsikuli.cmd -r C:\path\to\sikuli-script.sikuli & exit',
           shell=True)



回答2:


Although this does not answer the question directly, you can use this method to get the exit code indirectly in Python subprocess.

Create a batch file batch.cmd. In the file, call the command that you want to execute like this:

call %1 -r %2
EXIT /B %errorlevel%

In your python script:

import subprocess
exitcode = subprocess.call(r'C:\path\to\batch.cmd C:\path\to\runsikuli.cmd C:\path\to\sikuli-script.sikuli', shell=True)
print "Exit Code = "+str(exitcode)


来源:https://stackoverflow.com/questions/24555044/why-sometimes-python-subprocess-failed-to-get-the-correct-exit-code-after-runnin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!