summaryrefslogtreecommitdiffstats
path: root/Doc/library/atexit.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/atexit.rst')
-rw-r--r--Doc/library/atexit.rst8
1 files changed, 5 insertions, 3 deletions
diff --git a/Doc/library/atexit.rst b/Doc/library/atexit.rst
index f2dccc2..54131f5 100644
--- a/Doc/library/atexit.rst
+++ b/Doc/library/atexit.rst
@@ -67,8 +67,9 @@ 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:
+ with open("/tmp/counter") as infile:
+ _count = int(infile.read())
+ except FileNotFoundError:
_count = 0
def incrcounter(n):
@@ -76,7 +77,8 @@ making an explicit call into this module at termination. ::
_count = _count + n
def savecounter():
- open("/tmp/counter", "w").write("%d" % _count)
+ with open("/tmp/counter", "w") as outfile:
+ outfile.write("%d" % _count)
import atexit
atexit.register(savecounter)