Doug's just published a new PyMOTW, this one I didn't know about - the "atexit" module.
The atexit module defines a single function to register cleanup functions. Functions thus registered are automatically executed upon normal interpreter termination.
I'm sorta kicking myself now for not knowing that was there. Man.
See Doug's write up here: Doug Hellmann: PyMOTW: atexit
(Via Planet Python.)