file iterator - what happens here?

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri May 16 05:01:50 EDT 2003


Helmut Jarausch <jarausch at skynet.be> wrote in 
news:3ec4a51f$0$11146$ba620e4c at reader0.news.skynet.be:

> So, obviously the line 'line= cmd_input.readline()'
> disturbs the for iterator completely.
> Is that a feature?
> (This is Python cvs version from 2003/05/13)
> 
> Thanks for shedding some light on this mystery (at least for me)
> 

What happens is that to make the iterator work quickly, it reads a large 
chunk of the file and buffers it. The readline doesn't use the same buffer, 
so it gets data from the next part of the file (in your case past the end).

There are two ways to work around this. For Python 2.2.x, you should create 
an iterator on the file explicitly and call the 'next' method to get the 
additional lines:

cmd_input= file('Tree.inp','r')
cmd_input_iter = iter(cmd_input)
for line in cmd_input_iter:
     while  line.startswith('%') : # skip comment lines
         print "!!!",line
         line= cmd_input_iter.next()
     print "===",line,"<<<"
     if not line: break
cmd_input.close()

print "finished"

For Python 2.3 a file is its own iterator, so you can simply call the next 
method where you were using readline:

cmd_input= open('Tree.inp','r')
for line in cmd_input:
     while  line.startswith('%') : # skip comment lines
         print "!!!",line
         line= cmd_input.next()
     print "===",line,"<<<"
     if not line: break

cmd_input.close()

print "finished"

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list