From: Éric Araujo Date: Fri, 11 Mar 2011 16:42:48 +0000 (+0100) Subject: Use with statement where it improves the documentation (closes #10461) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a3dd56b6cffaeee21fec6529b212fd4fa98c1ea8;p=python Use with statement where it improves the documentation (closes #10461) --- diff --git a/Doc/library/atexit.rst b/Doc/library/atexit.rst index cc1051bf74..fc2b5a78b7 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 464764d793..b33d724618 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 b6ab342555..42c867d8b0 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 a0afe81ce0..505d30816f 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 603f143aa0..85c88dc23f 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