How to do this in Python?

Tim Chase python.list at tim.thechases.com
Tue Mar 17 18:34:37 EDT 2009


> 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








More information about the Python-list mailing list