[Tutor] A Really Quick Question

Kent Johnson kent37 at tds.net
Sat Oct 22 17:57:17 CEST 2005


Steve Haley wrote:
> I am running Python 2.1 which ships with ArcView 9.1.  I am going 
> through the 2.1 tutorial and came across readlines(). 
> 
> The tutorial seems to be telling me that ‘f.readlines(2) should read out 
> 2 bytes plus what is needed to complete the current line.  Instead, it 
> is reading out the entire file no matter what I enter as a parameter.  I 
> know I am going to feel really dumb when I hear the explanation but 
> please remember I’m new to this.

As the documentation says, the parameter to readlines() is a hint which may be rounded up to an internal buffer size. Python 2.4 seems to use a buffer size of 8192 so using sizehint < 8192 may have no effect.

If you want to read a single line of the file, use readline() instead. To iterate through all the lines of a file one at a time use
for line in f.xreadlines():
  # process line

HTH. Come back if you have more questions!
Kent



More information about the Tutor mailing list