If an exception is raised during execution of the exit handlers, a traceback is
printed (unless :exc:`SystemExit` is raised) and the exception information is
- saved. After all exit handlers have had a chance to run the last exception to
+ saved. After all exit handlers have had a chance to run, the last exception to
be raised is re-raised.
This function returns *func*, which makes it possible to use it as a
making an explicit call into this module at termination. ::
try:
- with open("counterfile") as infile:
+ with open('counterfile') as infile:
_count = int(infile.read())
except FileNotFoundError:
_count = 0
_count = _count + n
def savecounter():
- with open("counterfile", "w") as outfile:
- outfile.write("%d" % _count)
+ with open('counterfile', 'w') as outfile:
+ outfile.write('%d' % _count)
import atexit
+
atexit.register(savecounter)
Positional and keyword arguments may also be passed to :func:`register` to be
passed along to the registered function when it is called::
def goodbye(name, adjective):
- print('Goodbye, %s, it was %s to meet you.' % (name, adjective))
+ print('Goodbye %s, it was %s to meet you.' % (name, adjective))
import atexit
- atexit.register(goodbye, 'Donny', 'nice')
+ atexit.register(goodbye, 'Donny', 'nice')
# or:
atexit.register(goodbye, adjective='nice', name='Donny')
@atexit.register
def goodbye():
- print("You are now leaving the Python sector.")
+ print('You are now leaving the Python sector.')
This only works with functions that can be called without arguments.