Index: Doc/library/atexit.rst =================================================================== --- Doc/library/atexit.rst (revision 86561) +++ Doc/library/atexit.rst (working copy) @@ -66,20 +66,22 @@ automatically when the program terminates without relying on the application making an explicit call into this module at termination. :: - try: - _count = int(open("/tmp/counter").read()) - except IOError: - _count = 0 + try: + with open("/tmp/counter") as infile: + _count = int(infile.read()) + except IOError: + _count = 0 - def incrcounter(n): - global _count - _count = _count + n + def incrcounter(n): + global _count + _count = _count + n - def savecounter(): - open("/tmp/counter", "w").write("%d" % _count) + def savecounter(): + with open("/tmp/counter", "w") as outfile: + outfile.write("%d" % _count) - import atexit - atexit.register(savecounter) + 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::