summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2005-06-28 00:16:08 (GMT)
committerRaymond Hettinger <python@rcn.com>2005-06-28 00:16:08 (GMT)
commit02c64d5684ac8856dd8f0763324258c5cae6d45e (patch)
treed4db5bac4ef6a568c4a436d89627782bcc31bc38
parent5a34afb745f9279191d3220833193354fda18852 (diff)
downloadcpython-02c64d5684ac8856dd8f0763324258c5cae6d45e.zip
cpython-02c64d5684ac8856dd8f0763324258c5cae6d45e.tar.gz
cpython-02c64d5684ac8856dd8f0763324258c5cae6d45e.tar.bz2
Note that files 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 40ced7c..052e9d3 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}.