sys.stdout is not flushed

Jankins andyjian430074 at gmail.com
Tue Nov 24 03:14:56 CET 2009


On Nov 23, 4:08 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> Jankins schrieb:
>
>
>
>
>
> > I am trying to use sys.stdout to print out "process-bar" like:
> > -->1%
>
> > Here is my program ‘test.py’:
>
> > from sys import stdout
> > for v in range(10):
> >     stdout.write('-->%d' % v)
> >     stdout.flush()
> > else:
> >     stdout.write('done!')
> > #end for
>
> > Then, I use 'python -u test.py' to run this script. But what I get
> > is :
> > -->0-->1-->2-->3-->4-->5-->6-->7-->8-->9done!
>
> > I am suppose to get 'done!'.
>
> > Can anybody help me about this?
>
> You misunderstand what "flush" means. It is not about clearing the
> screen, or the line.
>
> Try printing
>
>    stdout.write('\r-->%d')
>
> Diez


But there is still a problem. When you use control character '\r', you
actually move to the head of the current buffer line and overwrite it.
So if I use this way:
for i in range(100, 0,-1)

The tail of the buffer is not overwrote.

How to solve this problem?

Thanks.



More information about the Python-list mailing list