Use with statement where it improves the documentation (closes #10461)
diff --git a/Doc/library/atexit.rst b/Doc/library/atexit.rst
index cc1051b..fc2b5a7 100644
--- a/Doc/library/atexit.rst
+++ b/Doc/library/atexit.rst
@@ -61,17 +61,22 @@
 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 464764d..b33d724 100644
--- a/Doc/library/cmd.rst
+++ b/Doc/library/cmd.rst
@@ -282,8 +282,8 @@
         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 b6ab342..42c867d 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -512,7 +512,8 @@
 
    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 a0afe81..505d308 100644
--- a/Doc/library/difflib.rst
+++ b/Doc/library/difflib.rst
@@ -750,8 +750,8 @@
        # 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,