]> granicus.if.org Git - python/commitdiff
#8522: use with statement instead of try-finally for file handling.
authorGeorg Brandl <georg@python.org>
Sun, 25 Apr 2010 10:17:27 +0000 (10:17 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 25 Apr 2010 10:17:27 +0000 (10:17 +0000)
Doc/howto/doanddont.rst

index f0c688bf2d169da3d7c740a7cf5278a8667ced4e..05d8df9db93b684ce4c665983a69aece1e4b1141 100644 (file)
@@ -232,11 +232,8 @@ file would not be closed when an exception is raised until the handler finishes,
 and perhaps not at all in non-C implementations (e.g., Jython). ::
 
    def get_status(file):
-       fp = open(file)
-       try:
+       with open(file) as fp:
            return fp.readline()
-       finally:
-           fp.close()
 
 
 Using the Batteries