atexit
provides the two functions: register
adds a function to a list of functions to be called when the Python script terminates
unregister
removes a function from this list import atexit import time from datetime import datetime def the_end(): print('the end has come.') atexit.register(the_end) for i in range(10): time.sleep(1) print( 10.0 / (i-5) )