# HG changeset patch # User Steven Kryskalla # Date 1354505319 28800 # Branch 2.7 # Node ID 5265e0858e89e57ce8fd786aced34fbfb78e0172 # Parent 48fbe78167cd185fe1cf6584d21d94ca2eb29e02 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 diff -r 48fbe78167cd -r 5265e0858e89 Doc/library/stdtypes.rst --- a/Doc/library/stdtypes.rst Sun Dec 02 13:13:56 2012 +0000 +++ b/Doc/library/stdtypes.rst Sun Dec 02 19:28:39 2012 -0800 @@ -2330,7 +2330,7 @@ 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:: @@ -2338,7 +2338,7 @@ f = open("hello.txt") try: for line in f: - print line + print line, finally: f.close() @@ -2392,7 +2392,7 @@ 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 diff -r 48fbe78167cd -r 5265e0858e89 Doc/tutorial/classes.rst --- a/Doc/tutorial/classes.rst Sun Dec 02 13:13:56 2012 +0000 +++ b/Doc/tutorial/classes.rst Sun Dec 02 19:28:39 2012 -0800 @@ -688,7 +688,7 @@ 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 diff -r 48fbe78167cd -r 5265e0858e89 Doc/tutorial/errors.rst --- a/Doc/tutorial/errors.rst Sun Dec 02 13:13:56 2012 +0000 +++ b/Doc/tutorial/errors.rst Sun Dec 02 19:28:39 2012 -0800 @@ -397,7 +397,7 @@ 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 @@ -407,7 +407,7 @@ 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 diff -r 48fbe78167cd -r 5265e0858e89 Doc/whatsnew/2.5.rst --- a/Doc/whatsnew/2.5.rst Sun Dec 02 13:13:56 2012 +0000 +++ b/Doc/whatsnew/2.5.rst Sun Dec 02 19:28:39 2012 -0800 @@ -598,7 +598,7 @@ 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 diff -r 48fbe78167cd -r 5265e0858e89 Doc/whatsnew/2.6.rst --- a/Doc/whatsnew/2.6.rst Sun Dec 02 13:13:56 2012 +0000 +++ b/Doc/whatsnew/2.6.rst Sun Dec 02 19:28:39 2012 -0800 @@ -282,7 +282,7 @@ 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