How do you register cleanup code to be run after script execution?

pxlpluker pxlpluker at cfl.rr.com
Mon May 10 17:41:36 EDT 2004


why not use
try:
    code
finally:
    cleanup code


Peter Otten wrote:

>use dmgass at hotmail dot com wrote:
>
>  
>
>>I'm writing a module and when it is imported by a script I want some code
>>automatically executed when the importing script is finished executing. 
>>I'd like it to execute before interactive mode is entered when executing
>>the
>>importing script from the command line.  I don't want to have to impose
>>that the importing script must call a function at it's end.
>>    
>>
>
>I think you want atexit.register():
>
><importing.py>
>print "in main"
>import imported
>raw_input("type enter to finish")
></importing.py>
>
><imported.py>
>import atexit
>
>def exitFunc(*args):
>    print "exitFunc callled with", args
>
>atexit.register(exitFunc, "with", "args")
>
>print "imported 'imported'"
></imported.py>
>
>$ python importing.py
>in main
>imported 'imported'
>type enter to finish
>exitFunc callled with ('with', 'args')
>$
>
>Peter
>
>  
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040510/6f2ce96c/attachment.html>


More information about the Python-list mailing list