summaryrefslogtreecommitdiffstats
path: root/Doc/library/atexit.rst
diff options
context:
space:
mode:
authorÉric Araujo <merwok@netwok.org>2011-03-11 16:42:48 (GMT)
committerÉric Araujo <merwok@netwok.org>2011-03-11 16:42:48 (GMT)
commita3dd56b6cffaeee21fec6529b212fd4fa98c1ea8 (patch)
tree6b9fe78b2e36d78d159216950039a033ba9311a8 /Doc/library/atexit.rst
parent17b880a5d6b64f1e7d26ec8229e8b41cdf740690 (diff)
downloadcpython-a3dd56b6cffaeee21fec6529b212fd4fa98c1ea8.zip
cpython-a3dd56b6cffaeee21fec6529b212fd4fa98c1ea8.tar.gz
cpython-a3dd56b6cffaeee21fec6529b212fd4fa98c1ea8.tar.bz2
Use with statement where it improves the documentation (closes #10461)
Diffstat (limited to 'Doc/library/atexit.rst')
-rw-r--r--Doc/library/atexit.rst9
1 files changed, 7 insertions, 2 deletions
diff --git a/Doc/library/atexit.rst b/Doc/library/atexit.rst
index cc1051b..fc2b5a7 100644
--- a/Doc/library/atexit.rst
+++ b/Doc/library/atexit.rst
@@ -61,17 +61,22 @@ from a file when it is imported and save the counter's updated value
automatically when the program terminates without relying on the application
making an explicit call into this module at termination. ::
+ infile = open("/tmp/counter")
try:
- _count = int(open("/tmp/counter").read())
+ _count = int(infile.read())
except IOError:
_count = 0
+ finally:
+ infile.close()
+
def incrcounter(n):
global _count
_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)