Was looking for python's equivalent to onexit and found this:
import atexit atexit.register(function_that_will_be_called_before_this_program_exits)
Name (required)
Mail (will not be published) (required)
Website
Notify me of follow-up comments by email.
Notify me of new posts by email.