0

假设我从终端运行以下程序,然后立即决定停止它,我将不得不按 control-c 5 次。我怎样才能做到让一个 control-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")
4

1 回答 1

3

将其包装在键盘中断异常中,并将 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")
于 2013-09-14T06:37:01.567 回答