]> granicus.if.org Git - python/commitdiff
Use with statement where it improves the documentation (closes #10461)
authorÉric Araujo <merwok@netwok.org>
Fri, 11 Mar 2011 16:42:48 +0000 (17:42 +0100)
committerÉric Araujo <merwok@netwok.org>
Fri, 11 Mar 2011 16:42:48 +0000 (17:42 +0100)
Doc/library/atexit.rst
Doc/library/cmd.rst
Doc/library/collections.rst
Doc/library/difflib.rst
Doc/tutorial/stdlib2.rst

index cc1051bf74ffa1064a209170b23a2d29123d2618..fc2b5a78b797654106ec2169ebf9710b2901932a 100644 (file)
@@ -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)
index 464764d7934ada9fbff7edf01afa72a2dddad6bb..b33d72461862eebb8e82bc9b21020f9bea957352 100644 (file)
@@ -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:
index b6ab342555bc1a08a0d89c6c69ce80cf63a60b9c..42c867d8b0a4f3a3b6a57eb911b18dec59411ab3 100644 (file)
@@ -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::
index a0afe81ce05e92fb1fa38c627ea4cd953070d7c5..505d30816fd7288fe7aba362b2378bd3fb7e6bb3 100644 (file)
@@ -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,
index 603f143aa0f0c38bb311b8e44c078141cd5b83f4..85c88dc23fcb60342e756dea3f355b458f183a6f 100644 (file)
@@ -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