Out of the box, atexit is unfortunately not oriented towards doingquite suited for what you want... to do: it's use is primarily used for resource cleanup at the very last moment, as things are shutting down and exiting. By analogy, it's the "finally" of a try/fexceptexcept, whereas what you want is the "else" of a try/except.
The simplest way would be to continueI can think of is continuing to use atexit, but create a global flag somewhere which you set only when your script "succeeds"... and then have all the functions you attach to atexitatexit check that flag, and do nothing unless it's been set.
Eg:
_success = False def atsuccess(func, *args, **kwds): def wrapper(): if _success: func(*args,**kwds) atexit(wrapper) def succeededset_success(): global _success _success = True # then call atsuccess() to attach your callbacks, # and call succeededset_success() before your script returns One limitation is if you have internalany code callingwhich calls sys.exit(0) before setting the success flag. ItSuch code should probably(probably) be refactored to return to the main function instead of callingfirst, so that you call set_success and sys.exit, but to make things still work in only one place. Failing that, you'dyou'll need to doadd something like the following wrapper around the main entry point in your script:
try: main() except SystemExit, err: if err.code == 0: succeededset_success() raise