如何重载键盘中断?(Python)
有没有办法让我在脚本运行时,当按下Ctrl+c
时,执行我的某个函数呢?
3 个回答
3
使用 KeyboardInterrupt 异常,然后在 except
块中调用你的函数。
9
当然可以。
try:
# Your normal block of code
except KeyboardInterrupt:
# Your code which is executed when CTRL+C is pressed.
finally:
# Your code which is always executed.