From a3dd56b6cffaeee21fec6529b212fd4fa98c1ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Fri, 11 Mar 2011 17:42:48 +0100 Subject: Use with statement where it improves the documentation (closes #10461) --- Doc/library/atexit.rst | 9 +++++++-- Doc/library/cmd.rst | 4 ++-- Doc/library/collections.rst | 3 ++- Doc/library/difflib.rst | 4 ++-- Doc/tutorial/stdlib2.rst | 4 +++- 5 files changed, 16 insertions(+), 8 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) diff --git a/Doc/library/cmd.rst b/Doc/library/cmd.rst index 464764d..b33d724 100644 --- a/Doc/library/cmd.rst +++ b/Doc/library/cmd.rst @@ -282,8 +282,8 @@ immediate playback:: def do_playback(self, arg): 'Playback commands from a file: PLAYBACK rose.cmd' self.close() - cmds = open(arg).read().splitlines() - self.cmdqueue.extend(cmds) + with open(arg) as f: + self.cmdqueue.extend(f.read().splitlines()) def precmd(self, line): line = line.lower() if self.file and 'playback' not in line: diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index b6ab342..42c867d 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -512,7 +512,8 @@ in Unix:: def tail(filename, n=10): 'Return the last n lines of a file' - return deque(open(filename), n) + with open(filename) as f: + return deque(f, n) Another approach to using deques is to maintain a sequence of recently added elements by appending to the right and popping to the left:: diff --git a/Doc/library/difflib.rst b/Doc/library/difflib.rst index a0afe81..505d308 100644 --- a/Doc/library/difflib.rst +++ b/Doc/library/difflib.rst @@ -750,8 +750,8 @@ It is also contained in the Python source distribution, as # we're passing these as arguments to the diff function fromdate = time.ctime(os.stat(fromfile).st_mtime) todate = time.ctime(os.stat(tofile).st_mtime) - fromlines = open(fromfile, 'U').readlines() - tolines = open(tofile, 'U').readlines() + with open(fromlines) as fromf, open(tofile) as tof: + fromlines, tolines = list(fromf), list(tof) if options.u: diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, diff --git a/Doc/tutorial/stdlib2.rst b/Doc/tutorial/stdlib2.rst index 603f143..85c88dc 100644 --- a/Doc/tutorial/stdlib2.rst +++ b/Doc/tutorial/stdlib2.rst @@ -141,7 +141,9 @@ standard size and in little-endian byte order:: import struct - data = open('myfile.zip', 'rb').read() + with open('myfile.zip', 'rb') as f: + data = f.read() + start = 0 for i in range(3): # show the first 3 file headers start += 14 -- cgit v0.12