Reputation: 101
I'm creating sort of a interactive command line in python. I have something like this:
def complete_menu():
while True:
cmd = input('cmd> ')
if cmd == "help":
print('help')
elif cmd == "?":
print('?')
When the user presses CTRL-C, instead of exiting the program I'm trying to make it so that it prints "please type exit to exit" and goes back to the while True
. I have something like this at the moment:
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print('Please use exit to exit')
complete_menu()
Although this works, there is a number of issues. For one, when CTRL-C is pressed the first time, it prints out the text and works perfectly. However, the second time the user presses CTRL-C, it exists with a bunch of messy text like any other program after pressing CTRL-C. Can this be fixed?
Upvotes: 4
Views: 1454
Reputation: 362517
The better way to do this is to register a signal handler:
import signal
def handler(signum, frame):
print("Please use exit to exit")
# or: just call sys.exit("goodbye")
...
def main():
signal.signal(signal.SIGINT, handler) # prevent "crashing" with ctrl+C
...
if __name__ == "__main__":
main()
Now when a Ctrl+C is received in your code, instead of a KeyboardInterrupt
exception being raised, the function handler
will be executed. This is a basic example, customize the code within handler to do what you want.
Note: My recommendation is to actually let the user exit with Ctrl+C, i.e. execute any cleanup code that you might need to run and then call sys.exit
here. Programs that require a stronger signal to kill are annoying.
Upvotes: 4