mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Drop double newlines printed in some file iteration examples.
Patch by Steven Kryskalla.
This commit is contained in:
parent
b7bc92530e
commit
bd5279ea24
4 changed files with 7 additions and 6 deletions
|
@ -2330,7 +2330,7 @@ Files have the following methods:
|
||||||
|
|
||||||
with open("hello.txt") as f:
|
with open("hello.txt") as f:
|
||||||
for line in 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
|
In older versions of Python, you would have needed to do this to get the same
|
||||||
effect::
|
effect::
|
||||||
|
@ -2338,7 +2338,7 @@ Files have the following methods:
|
||||||
f = open("hello.txt")
|
f = open("hello.txt")
|
||||||
try:
|
try:
|
||||||
for line in f:
|
for line in f:
|
||||||
print line
|
print line,
|
||||||
finally:
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
@ -2392,7 +2392,7 @@ Files have the following methods:
|
||||||
|
|
||||||
A file object is its own iterator, for example ``iter(f)`` returns *f* (unless
|
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
|
*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
|
: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
|
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
|
reading (behavior is undefined when the file is open for writing). In order to
|
||||||
|
|
|
@ -688,7 +688,7 @@ using a :keyword:`for` statement::
|
||||||
for char in "123":
|
for char in "123":
|
||||||
print char
|
print char
|
||||||
for line in open("myfile.txt"):
|
for line in open("myfile.txt"):
|
||||||
print line
|
print line,
|
||||||
|
|
||||||
This style of access is clear, concise, and convenient. The use of iterators
|
This style of access is clear, concise, and convenient. The use of iterators
|
||||||
pervades and unifies Python. Behind the scenes, the :keyword:`for` statement
|
pervades and unifies Python. Behind the scenes, the :keyword:`for` statement
|
||||||
|
|
|
@ -397,7 +397,7 @@ succeeded or failed. Look at the following example, which tries to open a file
|
||||||
and print its contents to the screen. ::
|
and print its contents to the screen. ::
|
||||||
|
|
||||||
for line in open("myfile.txt"):
|
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
|
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
|
amount of time after the code has finished executing. This is not an issue in
|
||||||
|
@ -407,7 +407,7 @@ ensures they are always cleaned up promptly and correctly. ::
|
||||||
|
|
||||||
with open("myfile.txt") as f:
|
with open("myfile.txt") as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
print line
|
print line,
|
||||||
|
|
||||||
After the statement is executed, the file *f* is always closed, even if a
|
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
|
problem was encountered while processing the lines. Other objects which provide
|
||||||
|
|
|
@ -546,6 +546,7 @@ Cédric Krier
|
||||||
Hannu Krosing
|
Hannu Krosing
|
||||||
Andrej Krpic
|
Andrej Krpic
|
||||||
Ivan Krstić
|
Ivan Krstić
|
||||||
|
Steven Kryskalla
|
||||||
Andrew Kuchling
|
Andrew Kuchling
|
||||||
Ralf W. Grosse-Kunstleve
|
Ralf W. Grosse-Kunstleve
|
||||||
Dave Kuhlman
|
Dave Kuhlman
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue