WINDOWS - python子进程返回错误的shell脚本退出代码

时间:2018-02-06 17:15:00

标签: python windows bash subprocess exit-code

嗨大家好我是脚本/蟒蛇新手,但我无休止地寻找无法解决的解决方案。

我正在尝试使用python(子进程)调用sh脚本(使用Windows 10)并将退出代码返回到python。

为清楚起见,我删除了除相关代码之外的所有内容。

我有一个sh脚本'return_value.sh',它有一行来设置退出代码:

#!/bin/sh
exit 9

我可以通过git-bash运行它,并按预期返回exit_code 9.

当我尝试从python中调用它时,它总是返回零:

import subprocess
p = subprocess.Popen(['C:\Program Files\Git\git-bash.exe','return_value.sh'], stdout=subprocess.PIPE)
p.wait()
out, err = p.communicate()
print (p.returncode)    

我搜索了很久但找不到分辨率。我能找到的最接近的是:

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

但它没有帮助我。有人可以给我一些建议吗?

非常感谢

麦克

0 个答案:

没有答案