Capture Control-C in Python

前端 未结 2 1477
我在风中等你
我在风中等你 2020-12-15 16:51

I want to know if it\'s possible to catch a Control-C in python in the following manner:

 if input != contr-c:
    #DO THINGS
 else:
    #quit
2条回答
  •  青春惊慌失措
    2020-12-15 17:13

    From your comments, it sounds like your only problem with except KeyboardInterrupt: is that you don't know how to make it exit when you get that interrupt.

    If so, that's simple:

    import sys
    
    try:
        user_input = input()
    except KeyboardInterrupt:
        sys.exit(0)
    

提交回复
热议问题