search a string

Carl J. Van Arsdall cvanarsdall at mvista.com
Thu Apr 20 19:00:36 EDT 2006


Bruno Desthuilliers wrote:
> 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:
>   
Ah sweet, I didn't realize you could do that, thanks!



> 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)
>
>   


-- 

Carl J. Van Arsdall
cvanarsdall at mvista.com
Build and Release
MontaVista Software




More information about the Python-list mailing list