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?