How do you register cleanup code to be run after script execution?
Peter Otten
__peter__ at web.de
Mon May 10 03:06:53 EDT 2004
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
More information about the Python-list
mailing list