global exception catching OR exitfunc

george young gry at ll.mit.edu
Thu Feb 27 11:07:07 EST 2003


On Wed, 26 Feb 2003 22:10:30 -0800
"Hilbert" <hilbert at microsoft.com> wrote:

> Is there a way to catch a "global" exception in a python
> program (meaning that if there would be an error
> during execution it could call a function before exiting)?
> 
> I have a program that creates large temp files and in case
> of any unforseen problems I still need to clean up before
> exiting.

It sounds like you want sys.exitfunc, e.g.:

def delete_temp_files():
	os.remove(temp1)
	os.remove(temp2)

import sys
sys.exitfunc = delete_temp_files


or in 2.x do something like:

def delete_temp_files(*tf):
	for f in tf:
		os.remove(f)
import atexit
atexit.register(delete_temp_files, temp1, temp1)

See:
   http://www.python.org/doc/2.3a2//lib/module-atexit.html

-- 
 I cannot think why the whole bed of the ocean is
 not one solid mass of oysters, so prolific they seem. Ah,
 I am wandering! Strange how the brain controls the brain!
	-- Sherlock Holmes in "The Dying Detective"




More information about the Python-list mailing list