[Python-checkins] cpython (3.3): #13510: clarify that f.readlines() is note necessary to iterate over a file.
ezio.melotti
python-checkins at python.org
Mon Apr 15 18:10:00 CEST 2013
http://hg.python.org/cpython/rev/1e8be05a4039
changeset: 83395:1e8be05a4039
branch: 3.3
parent: 83393:ad481c95a1d4
user: Ezio Melotti <ezio.melotti at gmail.com>
date: Mon Apr 15 19:08:31 2013 +0300
summary:
#13510: clarify that f.readlines() is note necessary to iterate over a file. Patch by Dan Riti.
files:
Doc/library/io.rst | 3 +++
Doc/tutorial/inputoutput.rst | 19 ++++---------------
Misc/ACKS | 1 +
3 files changed, 8 insertions(+), 15 deletions(-)
diff --git a/Doc/library/io.rst b/Doc/library/io.rst
--- a/Doc/library/io.rst
+++ b/Doc/library/io.rst
@@ -298,6 +298,9 @@
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*.
+ 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)
Change the stream position to the given byte *offset*. *offset* is
diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst
--- a/Doc/tutorial/inputoutput.rst
+++ b/Doc/tutorial/inputoutput.rst
@@ -300,18 +300,8 @@
>>> f.readline()
''
-``f.readlines()`` returns a list containing all the lines of data in the file.
-If given an optional parameter *sizehint*, it reads that many bytes from the
-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 reading lines from a file, you can loop over the file object. This is memory
+efficient, fast, and leads to simple code::
>>> for line in f:
... print(line, end='')
@@ -319,9 +309,8 @@
This is the first line of the file.
Second line of the file
-The alternative approach is simpler but does not provide as fine-grained
-control. Since the two approaches manage line buffering differently, they
-should not be mixed.
+If you want to read all the lines of a file in a list you can also use
+``list(f)`` or ``f.readlines()``.
``f.write(string)`` writes the contents of *string* to the file, returning
the number of characters written. ::
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1003,6 +1003,7 @@
Jean-Claude Rimbault
Vlad Riscutia
Wes Rishel
+Dan Riti
Juan M. Bello Rivas
Davide Rizzo
Anthony Roach
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list