在Ctrl-C上的Python中删除回溯 [英] Remove traceback in Python on Ctrl-C

查看:102
本文介绍了在Ctrl-C上的Python中删除回溯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您按 Ctrl + c 时,是否有一种方法可以防止回溯, 即在Python脚本中引发KeyboardInterrupt?

Is there a way to keep tracebacks from coming up when you hit Ctrl+c, i.e. raise KeyboardInterrupt in a Python script?

推荐答案

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..

这篇关于在Ctrl-C上的Python中删除回溯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆