Iterating over readlines() and map()

Christian Heimes lists at cheimes.de
Thu Mar 26 01:41:29 CET 2009


W. Martin Borgert schrieb:
> (Resend, because of funny error message:
>> Your mail to 'Python-list' with the subject
>>     Iterating over readlines() and map()
>> Is being held until the list moderator can review it for approval.
> Whatever this means.)
> 
> Hi,
> 
> if I understand correctly, this code would not read the complete file
> into the memory:
> 
> for line in myfile.readlines():
>     dosomethingwith(line)

No, you are wrong. file.readlines() reads the entire file into memory
and returns a list of strings. If you want to iterate over the lines of
a text file you can simply write:

for line in myfile:
    dosomethingwith(line)

It won't work for a binary file, though.

Christian




More information about the Python-list mailing list