键盘中断和os.system与subprocess.call

fac*_*cha 5 python operating-system subprocess

我正在python中编写一个小CLI(在cmd模块的帮助下).目前我正在尝试替换所有os.system出现的事件subprocess.call.

我面临的问题是,如果我运行外部脚本os.system,在我CTRL-C只打了一个子shell终止后(我回到我的CLI).当我用subprocess.call和运行相同的脚本时CTRL-C,脚本和我的CLI都终止执行.

有没有办法模仿这种os.system行为subprocess.call

Mar*_*ers 5

您可以使用异常处理程序捕获Python中的键盘中断:

try:
    retcode = subprocess.call(args)
except KeyboardInterrupt:
    pass # ignore CTRL-C
Run Code Online (Sandbox Code Playgroud)