]> granicus.if.org Git - python/commitdiff
Note that files are iterable.
authorRaymond Hettinger <python@rcn.com>
Tue, 28 Jun 2005 00:16:08 +0000 (00:16 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 28 Jun 2005 00:16:08 +0000 (00:16 +0000)
Doc/tut/tut.tex

index 40ced7cc6bbd552af95e7ec8de67c93d258d413e..052e9d3e7ea4c9ec8120730aaeaaf8d22249a4c0 100644 (file)
@@ -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}.