Python equivalent to Perls END block to cleanup after exit
Question: I have a script that may take a while to run. I would like it to save some
details to a file if it exits with an error.

In Perl, the END block would be the place to do something like that.

What is the Python way to clean up after exiting?


Answer: It can be done using the atexit module, as described here:
<https://docs.python.org/2/library/atexit.html>

    
    
    def savecounter():
        open("counter", "w").write("%d" % _count)
    
    import atexit
    atexit.register(savecounter)
    

