2016-09-14 120 views
1

我有python脚本(installer.py),它运行多个Python和shell程序:如何停止多python脚本运行,如果任何脚本有错误

print "Going to run Script1" 

os.system("python script1.py") 

print "Going to run Script2" 

os.system("python script2.py") 

但我发现,即使script1.py不因为它的错误而运行,它只是继续运行script2.py。

如何停止在script1.py本身的时间脚本(installer.py)..

+5

你为什么通过os.system脱壳,而不是直接导入脚本? –

+0

你可能想通过'try'和'except'查看错误处理。 – Ian

回答

0

os.system将返回系统调用的退出状态。只需检查您的命令是否正确执行。

ret = os.system('python script1.py') 
if ret != 0: 
    # call failed 
    raise Exception("System call failed with error code %d" % ret) 


ret = os.system('python script2.py') 
if ret != 0: 
    # call failed 
    raise Exception("System call failed with error code %d" % ret) 
+0

非常感谢你KernowBunney .. – suresh

+0

@suresh如果它解决了您的原始问题,请选择一个带勾号的答案。至于你的问题:这听起来更为复杂,应该作为一个新的堆栈溢出问题提出来(这不仅仅是一个python问题,而且还会影响系统管理的理由)。 – ccbunney