iterating over lines in a file
Daley, Mark W
mark.w.daley at intel.com
Fri Jul 21 18:42:17 EDT 2000
Isn't the argument for readlines() a memory size, as in 512 bytes? If so,
it is possible to have 3509 lines that are <= 512 bytes.
Just my two cents...
- Mark
-----Original Message-----
From: cjc26 at nospam.cornell.edu [mailto:cjc26 at nospam.cornell.edu]
Sent: Friday, July 21, 2000 1:32 PM
To: python-list at python.org
Subject: Re: iterating over lines in a file
* 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