Skip to content

Commit a3dd56b

Browse files
committed
Use with statement where it improves the documentation (closes python#10461)
1 parent 17b880a commit a3dd56b

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

Doc/library/atexit.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,22 @@ from a file when it is imported and save the counter's updated value
6161
automatically when the program terminates without relying on the application
6262
making an explicit call into this module at termination. ::
6363

64+
infile = open("/tmp/counter")
6465
try:
65-
_count = int(open("/tmp/counter").read())
66+
_count = int(infile.read())
6667
except IOError:
6768
_count = 0
69+
finally:
70+
infile.close()
71+
6872

6973
def incrcounter(n):
7074
global _count
7175
_count = _count + n
7276

7377
def savecounter():
74-
open("/tmp/counter", "w").write("%d" % _count)
78+
with open("/tmp/counter", "w") as outfile:
79+
outfile.write("%d" % _count)
7580

7681
import atexit
7782
atexit.register(savecounter)

Doc/library/cmd.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ immediate playback::
282282
def do_playback(self, arg):
283283
'Playback commands from a file: PLAYBACK rose.cmd'
284284
self.close()
285-
cmds = open(arg).read().splitlines()
286-
self.cmdqueue.extend(cmds)
285+
with open(arg) as f:
286+
self.cmdqueue.extend(f.read().splitlines())
287287
def precmd(self, line):
288288
line = line.lower()
289289
if self.file and 'playback' not in line:

Doc/library/collections.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,8 @@ in Unix::
512512

513513
def tail(filename, n=10):
514514
'Return the last n lines of a file'
515-
return deque(open(filename), n)
515+
with open(filename) as f:
516+
return deque(f, n)
516517

517518
Another approach to using deques is to maintain a sequence of recently
518519
added elements by appending to the right and popping to the left::

Doc/library/difflib.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,8 @@ It is also contained in the Python source distribution, as
750750
# we're passing these as arguments to the diff function
751751
fromdate = time.ctime(os.stat(fromfile).st_mtime)
752752
todate = time.ctime(os.stat(tofile).st_mtime)
753-
fromlines = open(fromfile, 'U').readlines()
754-
tolines = open(tofile, 'U').readlines()
753+
with open(fromlines) as fromf, open(tofile) as tof:
754+
fromlines, tolines = list(fromf), list(tof)
755755

756756
if options.u:
757757
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,

Doc/tutorial/stdlib2.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ standard size and in little-endian byte order::
141141

142142
import struct
143143

144-
data = open('myfile.zip', 'rb').read()
144+
with open('myfile.zip', 'rb') as f:
145+
data = f.read()
146+
145147
start = 0
146148
for i in range(3): # show the first 3 file headers
147149
start += 14

0 commit comments

Comments
 (0)