iterating over lines in a file

David Bolen db3l at fitlinxx.com
Fri Jul 21 14:14:04 EDT 2000


Donn Cave <donn at u.washington.edu> writes:

> Quoth David Bolen <db3l at fitlinxx.com>:
(...)
> | And only fully process files less than 8K in size.  The call to
> | file.readlines(8192) returns the list of lines contained within the
> | first 8K of the file (approximately), and that's all the 'for' is
> | going to iterate over.  You have to repeatedly call file.readlines()
> | again to keep reading the file, which puts you pretty much back in the
> | original readline() mode, just with bigger chunks.
> 
> Sure, but it's a useful idiom where the expected file is under
> ca. 500 bytes in any sane case.  The upper limit keeps the potential
> insane case from wiping out the program.  (Numbers arbitrary.)
> Not an approach we would always want to take, but there could be
> a place for it.

Oh sure - it's good as a safety valve - I was just pointing out that
it didn't address the underlying question in the thread which was to
iterate over all the lines of a file, regardless of size.  Under the
conditions you note (small file, don't want to risk a large one
causing problems and don't mind ignoring the overflow) it's a
perfectly good idiom and safer than just a blind readlines().

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list