Commit 1b332065 authored by Ezio Melotti's avatar Ezio Melotti

#13510: clarify that f.readlines() is note necessary to iterate over a file. Patch by Dan Riti.

parent 12ec99d8
...@@ -296,6 +296,9 @@ I/O Base Classes ...@@ -296,6 +296,9 @@ I/O Base Classes
to control the number of lines read: no more lines will be read if the to control the number of lines read: no more lines will be read if the
total size (in bytes/characters) of all lines so far exceeds *hint*. total size (in bytes/characters) of all lines so far exceeds *hint*.
Note that it's already possible to iterate on file objects using ``for
line in file: ...`` without calling ``file.readlines()``.
.. method:: seek(offset, whence=SEEK_SET) .. method:: seek(offset, whence=SEEK_SET)
Change the stream position to the given byte *offset*. *offset* is Change the stream position to the given byte *offset*. *offset* is
......
...@@ -295,18 +295,8 @@ containing only a single newline. :: ...@@ -295,18 +295,8 @@ containing only a single newline. ::
>>> f.readline() >>> f.readline()
'' ''
``f.readlines()`` returns a list containing all the lines of data in the file. For reading lines from a file, you can loop over the file object. This is memory
If given an optional parameter *sizehint*, it reads that many bytes from the efficient, fast, and leads to simple code::
file and enough more to complete a line, and returns the lines from that. This
is often used to allow efficient reading of a large file by lines, but without
having to load the entire file in memory. Only complete lines will be returned.
::
>>> f.readlines()
['This is the first line of the file.\n', 'Second line of the file\n']
An alternative approach to reading lines is to loop over the file object. This is
memory efficient, fast, and leads to simpler code::
>>> for line in f: >>> for line in f:
print line, print line,
...@@ -314,9 +304,8 @@ memory efficient, fast, and leads to simpler code:: ...@@ -314,9 +304,8 @@ memory efficient, fast, and leads to simpler code::
This is the first line of the file. This is the first line of the file.
Second line of the file Second line of the file
The alternative approach is simpler but does not provide as fine-grained If you want to read all the lines of a file in a list you can also use
control. Since the two approaches manage line buffering differently, they ``list(f)`` or ``f.readlines()``.
should not be mixed.
``f.write(string)`` writes the contents of *string* to the file, returning ``f.write(string)`` writes the contents of *string* to the file, returning
``None``. :: ``None``. ::
......
...@@ -838,6 +838,7 @@ Nicholas Riley ...@@ -838,6 +838,7 @@ Nicholas Riley
Jean-Claude Rimbault Jean-Claude Rimbault
Vlad Riscutia Vlad Riscutia
Wes Rishel Wes Rishel
Dan Riti
Juan M. Bello Rivas Juan M. Bello Rivas
Davide Rizzo Davide Rizzo
Anthony Roach Anthony Roach
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment