Python- How to check if program gets aborted by user while running?
Question: If I am running a python program on linux terminal and i abort it manually by
pressing ctrl+c, how can i make my program do something when this event
occurs.

something like:

    
    
    if sys.exit():
        print "you chose to end the program"
    


Answer: Well, you can use `KeyBoardInterrupt`, using a try-except block:

    
    
    try:
        # some code here
    except KeyboardInterrupt:
        print "You exited
    

Try the following in your command line:

    
    
    import time
    
    try:
        while True:
            time.sleep(1)
            print "Hello"
    except KeyboardInterrupt:
        print "No more Hellos"
    

