How to do this in Python?

Jim Garrison jhg at acm.org
Tue Mar 17 21:00:51 EDT 2009


andrew cooke wrote:
> Jim Garrison wrote:
>> I'm an experienced C/Java/Perl developer learning Python.
>>
>> What's the canonical Python way of implementing this pseudocode?
[snip]

> 
> embarrassed by the other reply i have read, 

There's always some "trollish" behavior in any comp.lang.*
group.  Too many people treat languages as religions instead
of tools.  They all have strengths and weaknesses :-)

> but not doing much binary i/o
> myself, i suggest:
> 
> with open(...) as f:
>   while (True):
>     buf = f.read(10000)
>     if not buf: break
>     ...
> 
> but are you sure you don't want:
> 
> with open(...) as f:
>   for line in f:
>     ...
> 
> andrew

For a one-off,,your first example would work fine.  See the
other reply from Tim Chase for a much more Pythonesque
pattern.  I don't want "for line in f:" because binary
files don't necessarily have lines and I'm bulk processing
files potentially 100MB and larger.  Reading them one line
at a time would be highly inefficient.

Thanks



More information about the Python-list mailing list