Skip to content
Advertisement

How to prevent Python script from Force exit?

I am running a python script and trying to prevent from force exiting or print an error message or log that into the log file.

I am already using logging.info("") for logging.. or just print for printing something..

but what or how do I create a method or function that can either do print or log when it force exit?

For example, if my test.py is running and I press Ctrl + C to exit out.. I want to log that or print out..

JavaScript

I am using above coding for logging for when I want to exit the program. But this doesn’t deal with something like ctrl + c I want to also log just in case program exit by accident or something

Advertisement

Answer

UPDATED

Use try and except:

JavaScript

You could also leverage the atexit module to execute a function when the script exits.

JavaScript

https://docs.python.org/2/library/atexit.html

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement