Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sys.stdout is not flushed

9 views
Skip to first unread message

Jankins

unread,
Nov 23, 2009, 4:57:23 PM11/23/09
to
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?

Thanks.

Jankins

Diez B. Roggisch

unread,
Nov 23, 2009, 5:08:25 PM11/23/09
to
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

Jankins

unread,
Nov 23, 2009, 5:59:39 PM11/23/09
to
On Nov 23, 4:08 pm, "Diez B. Roggisch" <de...@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

It works. I did misunderstood the meaning the 'flush'.
Thanks so much.

Jankins

unread,
Nov 23, 2009, 9:14:56 PM11/23/09
to
On Nov 23, 4:08 pm, "Diez B. Roggisch" <de...@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.

Cousin Stanley

unread,
Nov 23, 2009, 9:32:50 PM11/23/09
to

>> ....

>> 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.
> ....

The following version works ok for me
using python2.5 under debian linux ....

import sys
import time

print

for n in range( 11 ) :
sys.stdout.write( '\r Working ----> %d ' % n )
sys.stdout.flush()
time.sleep( 1 )

else :
print "\n"
print " That's all, folks !"
print " Adios ........... "


--
Stanley C. Kitching
Human Being
Phoenix, Arizona

Dave Angel

unread,
Nov 23, 2009, 9:54:56 PM11/23/09
to Jankins, pytho...@python.org

No idea what you mean by "buffer line." This is moving the cursor
around on the console.

Anyway, for such a loop, just make sure all the strings are the same
length. Or just cheat and always write a few blanks at the end.

sys.stdout.write("\r -- %5d" % i)

should do it, for up to 5 digit values

DaveA

Jankins

unread,
Nov 24, 2009, 12:09:36 AM11/24/09
to

Thanks. It works. Put some space at the end of the output string.

Jankins

unread,
Nov 24, 2009, 12:11:04 AM11/24/09
to

'%5d' is elegant. I prefer adding some space at the end of the output
string.
Thanks.

Nobody

unread,
Nov 24, 2009, 1:19:00 AM11/24/09
to
On Mon, 23 Nov 2009 23:08:25 +0100, Diez B. Roggisch wrote:

> Try printing
>
> stdout.write('\r-->%d')

^M-->0^M-->1^M-->2^M-->3... ;)

But it's probably good enough for the OP's purposes.

0 new messages