python进程已结束、退出代码0_为什么有时Python子进程在运行进程后无法获取正确的退出代码?...

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)

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

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)

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?

解决方案

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值