5

我正在用 python 编写一个小型 CLI(在 cmd 模块的帮助下)。目前我正在尝试os.systemsubprocess.call.

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

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

4

1 回答 1

5

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

try:
    retcode = subprocess.call(args)
except KeyboardInterrupt:
    pass # ignore CTRL-C
于 2013-09-11T11:41:12.073 回答