用python运行bash脚本
我编写了python脚本来运行bash脚本,它使用以下行运行:用python运行bash脚本,python,bash,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") 现在,当ba
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并没有继续它所做的事情,它只是结束了。如何使python脚本在bash失败后继续运行?您忘记了
通信。此外,当bash脚本失败时,您将返回,难怪python“只是停止”
您是否尝试过try/except
及其作用?获取错误的堆栈跟踪。但是我意识到当returncode!=0
。。。难怪它“刚刚结束”,正如文档已经告诉您的那样,如果您可以使用更高级别的函数之一为您处理这些细节,那么就不要使用barePopen
。在这种情况下,check\u call
或run(…,check=True)
将在子流程失败时引发异常。
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")