How to do this in Python?

Jim Garrison jhg at acm.org
Tue Mar 17 20:54:54 EDT 2009


Tim Chase wrote:
>> Am I missing something basic, or is this the canonical way:
>>
>>      with open(filename,"rb") as f:
>>          buf = f.read(10000)
>>          while len(buf) > 0
>>              # do something....
>>              buf = f.read(10000)
> 
> That will certainly do.  Since read() should simply return a 0-length
> string when you're sucking air, you can just use the test "while buf"
> instead of "while len(buf) > 0".
> 
> However, if you use it multiple places, you might consider writing an
> iterator/generator you can reuse:
> 
>   def chunk_file(fp, chunksize=10000):
>     s = fp.read(chunksize)
>     while s:
>       yield s
>       s = fp.read(chunksize)
> 
>   with open(filename1, 'rb') as f:
>     for portion in chunk_file(f):
>       do_something_with(portion)
> 
>   with open(filename2, 'rb') as f:
>     for portion in chunk_file(f, 1024):
>       do_something_with(portion)
> 
> -tkc

Ah.  That's the Pythonesque way I was looking for.  I knew
it would be a generator/iterator but haven't got the Python
mindset down yet and haven't played with writing my own
generator.  I'm still trying to think in purely object-
oriented terms where I would override __next__() to
return a chunk of the appropriate size.

Give a man some code and you solve his immediate problem.
Show him a pattern and you've empowered him to solve
his own problems.  Thanks!



More information about the Python-list mailing list