For instance, let the program write down a log file before I manually stop it.
Asked
Active
Viewed 213 times
1
-
Possible duplicate of [How to exit from Python without traceback?](http://stackoverflow.com/questions/1187970/how-to-exit-from-python-without-traceback) – McGrady Feb 27 '17 at 05:58
-
1@McGrady I don't think it's a duplicate -- the question you refer to is about exiting without traceback; this one is about having exit hook. – avysk Feb 27 '17 at 05:59
-
2This question is in a Review Queue. I voted to close as "Unclear what you are asking". I think you can salvage the question by providing more details. If English is a second language, then use Google translator. We can clean up the English. – jww Feb 27 '17 at 06:11
1 Answers
4
You can use atexit
module: https://docs.python.org/3.5/library/atexit.html
Example:
import atexit
import time
def exit_hook():
print("Exiting!")
atexit.register(exit_hook)
while True:
print("looping...")
time.sleep(1)

avysk
- 1,973
- 12
- 18