Do something at the end of a python program

I want to make a python script do something like post a message before it ends. I tried signal and it works when I type Ctrl+c in cmd window. However, it seems to be inoperative when I just close the cmd window or kill the process from the Windows task manager. So, how can I achieve my goal in the state of the situation above? Thanks for all the suggestions!

+5
source share
1 answer

Take a look at the atexit module:

http://docs.python.org/library/atexit.html

Example:

 import atexit def exit_handler(): print('My program just quit') atexit.register(exit_handler) 
+5
source

Source: https://habr.com/ru/post/1265239/


All Articles