"Faster" I/O in a script

Gary Herron gherron at islandtraining.com
Mon Jun 2 20:52:11 EDT 2008


miller.paul.w at gmail.com wrote:
> On Jun 2, 2:08 am, "kalakouentin" <kalakouen... at yahoo.com> wrote:
>
>   
>>  Do you know a way to actually load my data in a more
>> "batch-like" way so I will avoid the constant line by line reading?
>>     
>
> If your files will fit in memory, you can just do
>
> text = file.readlines()
>
> and Python will read the entire file into a list of strings named
> 'text,' where each item in the list corresponds to one 'line' of the
> file.
>   

No that won't help.  That has to do *all* the same work (reading blocks 
and finding line endings) as the iterator PLUS allocate and build a list. 

Better to just use the iterator.

for line in file:
  ...

Gary Herron

> --
> http://mail.python.org/mailman/listinfo/python-list
>   




More information about the Python-list mailing list