且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Ctrl-C上的Python中删除回溯

更新时间:2023-12-02 19:11:58

import sys
try:
    # your code
except KeyboardInterrupt:
    sys.exit(0) # or 1, or whatever

是最简单的方法,假设您仍然想在获得 Ctrl + c 时退出.

Is the simplest way, assuming you still want to exit when you get a Ctrl+c.

如果您想不使用try/except来捕获它,则可以使用 signal模块 ="="这样的食谱,除了它似乎不适用于Windows.

If you want to trap it without a try/except, you can use a recipe like this using the signal module, except it doesn't seem to work for me on Windows..