How to do this in Python?

Mel mwilson at the-wire.com
Wed Mar 18 11:10:40 EDT 2009


Jim Garrison wrote:
> 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?
[ ... ]
>> but not doing much binary i/o
>> myself, i suggest:
>> 
>> with open(...) as f:
>>   while (True):
>>     buf = f.read(10000)
>>     if not buf: break
>>     ...
[ ... ]

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

It would be more work, but subclassing the file class, with a next method 
yielding the binary record you want would be fairly clean.

	Mel.






More information about the Python-list mailing list