python - 使用 python 运行 bash 脚本
问题描述
我编写了 python 脚本来运行 bash 脚本,它使用以下行运行它:
result = subprocess.Popen(['./test.sh %s %s %s' %(input_file, output_file, master_name)], shell = True)
if result != 0:
print("Sh*t hits the fan at some point")
return
else:
print("Moving further")
现在当 bash 脚本失败时我遇到了麻烦,python 不会继续做它正在做的事情,它只会结束。我怎样才能让它在 bash 失败后继续运行 python 脚本?
解决方案
你忘了communicate
. 除了你return
,当 bash 脚本失败时,难怪 python “停止”。
from subprocess import Popen, PIPE
p = Popen(..., stdout=PIPE, stderr=PIPE)
output, error = p.communicate()
if p.returncode != 0:
print("Sh*t hits the fan at some point %d %s %s" % (p.returncode, output, error))
print("Movign further")
推荐阅读
- c++ - 公共标头未找到其他公共标头
- mysql - 在mysql中填充/连接多对多关系表的多个表
- angular - 卸载前调用时无法发布 httpClient 请求
- c# - 无状态声明
- angular - Angular 7 - 有两个不同formGroups的formControls具有相同的formControlName?
- javascript - 使用 MongoDB 和 Nodejs 创建嵌套的子文档/子元素
- xml - 如何使用 PowerShell 从 XML 文件中获取退出代码?
- swift - 如何在 Xcode swift 中以编程方式更改 UIBarButtunItem 功能
- postfix-mta - 如何将 Postfix 配置为仅中继来自特定域的电子邮件?
- python-3.x - 不止一种方法对硒元素进行定位