search a string

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Apr 20 22:11:01 EDT 2006


Carl J. Van Arsdall a écrit :
(snip)

> With a file object, to iterate through the lines in a file don't you 
> have to use readlines()?

Nope - no more, should I say. The file object is now an iterable. 
file.readlines() try and read all the file and returns it as a list. 
It's ok for small files, but can lead to problem with huge ones... using 
the file as an iterator, you have a lazy read - just like you would with 
the older file.readline() idiom:

line = f.readline()
while line:
   do_something_with(line)
   line = f.readline()

but with much less hassle :

for line in f:
   do_something_with(line)




More information about the Python-list mailing list