How to do this in Python?

Grant Edwards grante at visi.com
Wed Mar 18 05:05:02 CET 2009


On 2009-03-18, Jim Garrison <jhg at acm.org> wrote:
> 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.

That's not pythonic unless you really do need to use
chumk_file() in a lot of places (IMO, more than 3 or 4).  If it
only going to be used once, then just do the usual thing:

f = open(...)
while True:
   buf = f.read()
   if not buf: break
   # whatever.
f.close()

Or, you can substitute a with if you want.

-- 
Grant




More information about the Python-list mailing list