Steven Kryskalla avatar Steven Kryskalla committed 5265e08

Doc fixes for looping over a file's lines (for 2.7)

Fixed a few places in the docs where double newlines were being printed when
looping over a file line-by-line.
* * *
tmp

Comments (0)

Files changed (5)

Doc/library/stdtypes.rst

 
       with open("hello.txt") as f:
           for line in f:
-              print line
+              print line,
 
    In older versions of Python, you would have needed to do this to get the same
    effect::
       f = open("hello.txt")
       try:
           for line in f:
-              print line
+              print line,
       finally:
           f.close()
 
 
    A file object is its own iterator, for example ``iter(f)`` returns *f* (unless
    *f* is closed).  When a file is used as an iterator, typically in a
-   :keyword:`for` loop (for example, ``for line in f: print line``), the
+   :keyword:`for` loop (for example, ``for line in f: print line.strip()``), the
    :meth:`~file.next` method is called repeatedly.  This method returns the next input
    line, or raises :exc:`StopIteration` when EOF is hit when the file is open for
    reading (behavior is undefined when the file is open for writing).  In order to

Doc/tutorial/classes.rst

    for char in "123":
        print char
    for line in open("myfile.txt"):
-       print line
+       print line,
 
 This style of access is clear, concise, and convenient.  The use of iterators
 pervades and unifies Python.  Behind the scenes, the :keyword:`for` statement

Doc/tutorial/errors.rst

 and print its contents to the screen. ::
 
    for line in open("myfile.txt"):
-       print line
+       print line,
 
 The problem with this code is that it leaves the file open for an indeterminate
 amount of time after the code has finished executing. This is not an issue in
 
    with open("myfile.txt") as f:
        for line in f:
-           print line
+           print line,
 
 After the statement is executed, the file *f* is always closed, even if a
 problem was encountered while processing the lines. Other objects which provide

Doc/whatsnew/2.5.rst

 
    with open('/etc/passwd', 'r') as f:
        for line in f:
-           print line
+           print line,
            ... more processing code ...
 
 After this statement has executed, the file object in *f* will have been

Doc/whatsnew/2.6.rst

 
    with open('/etc/passwd', 'r') as f:
        for line in f:
-           print line
+           print line,
            ... more processing code ...
 
 After this statement has executed, the file object in *f* will have been
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.