📜  如何在 python 代码示例中捕获 ctrl c

📅  最后修改于: 2022-03-11 14:45:53.335000             🧑  作者: Mango

代码示例3
import signal
import time
 
def handler(signum, frame):
    res = input("Ctrl-c was pressed. Do you really want to exit? y/n ")
    if res == 'y':
        exit(1)
 
signal.signal(signal.SIGINT, handler)
 
count = 0
while True:
    print(count)
    count += 1
    time.sleep(0.1)