Use with statement where it improves the documentation (closes #10461) · pythoncapi/cpython@a3dd56b · GitHub
Skip to content

Commit a3dd56b

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

5 files changed

Lines changed: 16 additions & 8 deletions

File tree

Doc/library/atexit.rst

Lines changed: 7 additions & 2 deletions

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

0 commit comments

Comments
 (0)