Strange loop behavior

Arnaud Delobelle arnodel at
Wed Mar 26 09:47:17 CET 2008

On Mar 26, 8:35 am, Gabriel Rossetti
<gabriel.rosse... at> 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(
>     while d != "":
>         file_str.write(d)
>         d = repr(
> I also tried writing the while's condition like so : len(d) > 0, but
> that doesn't change anything. I tried step-by-step debugging using
> PyDev(eclipse plugin) and I noticed this, once the while was read once,
> it is never re-read, basically, the looping does happen, but just in
> between the two lines in the loop's body/block, it never goes on the
> while and thus never verifies the condition and thus loops forever. I
> had been using psyco (a sort of JIT for python) and so I uninstalled it
> and restarted eclipse and I still get the same thing. This looks like
> some bug, but I may be wrong, does anybody understand what's going on here?
> Thanks,
> Gabriel
> PS
> And yes I checked, the "d" variable is en empty string at some point, so
> the looping should stop.

No, it isn't the empty string, it is the printable representation of
the empty string (because you wrap your calls in repr()),
which is the string "''":

    >>> print "''"

Why do you wrap in the repr() function?  If you remove the
two repr() I suspect your code will work.

OTOH do you know that the read() method of file objects does what you
want?  You could simply write:

    file_str =

Or are there some other factors that prevent you from doing this?


More information about the Python-list mailing list