iterating over lines in a file

David Bolen db3l at fitlinxx.com
Thu Jul 20 17:49:10 EDT 2000


cjc26 at nospam.cornell.edu (Cliff Crawford) writes:

> readlines() has an optional argument which specifies the approximate
> number of bytes to read in at a time, rather than the entire file.
> So something like
> 
> for line in file.readlines(8192):
>     # process line
> 
> would only use about 8k of memory.

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.

--
-- 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