summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2005-06-28 00:18:10 (GMT)
committerRaymond Hettinger <python@rcn.com>2005-06-28 00:18:10 (GMT)
commitf7955af62f83dd44c4c6d9b495c0096cf4a8cd74 (patch)
tree0fd8240aa45603c22cdd09eb8f8466da42ed1879
parent85ca29a45ac6c34f2e5248e90c6007ae5f79c480 (diff)
downloadcpython-f7955af62f83dd44c4c6d9b495c0096cf4a8cd74.zip
cpython-f7955af62f83dd44c4c6d9b495c0096cf4a8cd74.tar.gz
cpython-f7955af62f83dd44c4c6d9b495c0096cf4a8cd74.tar.bz2
Note that file objects are iterable.
-rw-r--r--Doc/tut/tut.tex15
1 files changed, 15 insertions, 0 deletions
diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex
index 7c41a89..09fd2f0 100644
--- a/Doc/tut/tut.tex
+++ b/Doc/tut/tut.tex
@@ -3166,6 +3166,21 @@ entire file in memory. Only complete lines will be returned.
['This is the first line of the file.\n', 'Second line of the file\n']
\end{verbatim}
+An alternate approach to reading lines is to loop over the file object.
+This is memory efficient, fast, and leads to simpler code:
+
+\begin{verbatim}
+>>> for line in f:
+ print line,
+
+This is the first line of the file.
+Second line of the file
+\end{verbatim}
+
+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.
+
\code{f.write(\var{string})} writes the contents of \var{string} to
the file, returning \code{None}.