Reading from file up to arbitrary byte.

Peter Hansen peter at engcorp.com
Fri Feb 13 17:27:24 EST 2004


"David M. Wilson" wrote:
> 
> Is there a simple way of reading from a file object up to a specific
> byte value? I would like to do this without reading one character at a
> time, or reading in chunks and holding a remainder over.

I'd say the simple way to do it would be to read in chunks and hold a 
remainder over.  It's not complicated: why don't you like that way?

Or even simpler: just use read()[:chunksize] and not worry about the
fact that you're reading all the data and throwing some away.
Performance-wise, this probably beats the pants off most alternatives,
if performance is what concerns you, and unless your file is really 
big and chunksize is small, who cares about the memory that is wasted
for a few microseconds?

It might also help respondents if you describe the reason for wanting
to read the first part of the file like that.  Maybe there's a more
suitable approach.

-Peter



More information about the Python-list mailing list