Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python运行bash脚本_Python_Bash - Fatal编程技术网

用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

我编写了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并没有继续它所做的事情,它只是结束了。如何使python脚本在bash失败后继续运行?

您忘记了
通信。此外,当bash脚本失败时,您将返回
,难怪python“只是停止”


您是否尝试过
try/except
及其作用?获取错误的堆栈跟踪。但是我意识到当
returncode!=0
。。。难怪它“刚刚结束”,正如文档已经告诉您的那样,如果您可以使用更高级别的函数之一为您处理这些细节,那么就不要使用bare
Popen
。在这种情况下,
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")