iterating over lines in a file
Cliff Crawford
cjc26 at nospam.cornell.edu
Fri Jul 21 16:32:29 EDT 2000
* David Bolen <db3l at fitlinxx.com> menulis:
|
| > 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.
That doesn't seem to be true--readlines() reads the whole file whether
you specify a size argument or not. For example:
>>> f=open("file.txt")
>>> size=0
>>> for line in f.readlines():
... size=size+len(line)
...
>>> size
3509
>>> f.close()
>>> f=open("file.txt")
>>> size=0
>>> for line in f.readlines(512):
... size=size+len(line)
...
>>> size
3509
--
cliff crawford -><- http://www.people.cornell.edu/pages/cjc26/
Synaesthesia now! icq 68165166
More information about the Python-list
mailing list