sys.stdout is not flushed

J

Jankins

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
 
D

Diez B. Roggisch

Jankins said:
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
 
J

Jankins

Jankins schrieb:











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

Jankins

Jankins schrieb:











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

Cousin Stanley

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

Dave Angel

Jankins said:
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.
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
 
J

Jankins

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

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

Jankins

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top