How to do this in Python?
Luis Zarrabeitia
kyrie at uh.cu
Tue Mar 17 18:35:50 EDT 2009
On Tuesday 17 March 2009 06:04:36 pm Jim Garrison 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)
well, a bit more canonical would be:
...
while buf:
# do something
...
instead of comparing len(buf) with 0. But that's a minor detail.
One could use this:
with open(filename, "rb") as f:
for buf in iter(lambda: f.read(1000),''):
do_something(buff)
but I don't really like a lambda in there. I guess one could use
functools.partial instead, but it still looks ugly to me. Oh, well, I guess I
also want to see the canonical way of doing it.
--
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie
More information about the Python-list
mailing list