Strange loop behavior
Matt Nordhoff
mnordhoff at mattnordhoff.com
Wed Mar 26 05:00:40 EDT 2008
Gabriel Rossetti wrote:
> Hello,
>
> I wrote a program that reads data from a file and puts it in a string,
> the problem is that it loops infinitely and that's not wanted, here is
> the code :
>
> d = repr(f.read(DEFAULT_BUFFER_SIZE))
> while d != "":
> file_str.write(d)
> d = repr(f.read(DEFAULT_BUFFER_SIZE))
FWIW, you could do it like this to avoid duplicating that line of code:
while True:
d = f.read(DEFAULT_BUFFER_SIZE)
if not d:
break
file_str.write(d)
Some people would also compress the whitespace a bit:
while True:
d = f.read(DEFAULT_BUFFER_SIZE)
if not d: break
file_str.write(d)
<snip>
(I'll comment on your Unicode issues in a minute.)
--
More information about the Python-list
mailing list