假设我从终端运行以下程序,然后立即决定停止它,我将不得不按下控件-c 5次。我如何使它这样一个控制-c将退出整个程序?
os.system("python run_me1.py -lines -s {0} -u {1}".format(args.start, args.until))
os.system("python run_me2.py -derivs -tt")
if args.mike: os.system("python run_me3.py -f derivs.csv tt.csv")
os.system("gnumeric derivs.csv")
os.system("gnumeric tt.csv")发布于 2013-09-14 06:37:01
将其包装在键盘中断异常中,并将os.system替换为subprocess.call。
请不要为了路径解析的方便而将shell=True参数放入其中,但这具有安全性含义,在执行此操作之前,您应该将其失效。
import subprocess
try:
subprocess.call("python run_me1.py -lines -s {0} -u {1}".format(args.start, args.until), shell=True)
subprocess.call("python run_me2.py -derivs -tt", shell=True)
if args.mike: subprocess.call("python run_me3.py -f derivs.csv tt.csv", shell=True)
subprocess.call("gnumeric derivs.csv", shell=True)
subprocess.call("gnumeric tt.csv", shell=True)
except KeyboardInterrupt:
print("exiting early")https://stackoverflow.com/questions/18799021
复制相似问题