Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

sys.stdout is not flushed

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


andyjian430074 at gmail

Nov 23, 2009, 1:57 PM

Post #1 of 9 (523 views)
Permalink
sys.stdout is not flushed

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
--
http://mail.python.org/mailman/listinfo/python-list


deets at nospam

Nov 23, 2009, 2:08 PM

Post #2 of 9 (499 views)
Permalink
Re: sys.stdout is not flushed [In reply 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
--
http://mail.python.org/mailman/listinfo/python-list


andyjian430074 at gmail

Nov 23, 2009, 2:59 PM

Post #3 of 9 (493 views)
Permalink
Re: sys.stdout is not flushed [In reply 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.
--
http://mail.python.org/mailman/listinfo/python-list


andyjian430074 at gmail

Nov 23, 2009, 6:14 PM

Post #4 of 9 (491 views)
Permalink
Re: sys.stdout is not flushed [In reply 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.
--
http://mail.python.org/mailman/listinfo/python-list


cousinstanley at gmail

Nov 23, 2009, 6:32 PM

Post #5 of 9 (492 views)
Permalink
Re: sys.stdout is not flushed [In reply 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

--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Nov 23, 2009, 6:54 PM

Post #6 of 9 (487 views)
Permalink
Re: sys.stdout is not flushed [In reply to]

Jankins wrote:
> 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.
>
>
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

--
http://mail.python.org/mailman/listinfo/python-list


andyjian430074 at gmail

Nov 23, 2009, 9:09 PM

Post #7 of 9 (493 views)
Permalink
Re: sys.stdout is not flushed [In reply to]

On Nov 23, 8:32 pm, Cousin Stanley <cousinstan...@gmail.com> wrote:
> >> ....
> >> 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

Thanks. It works. Put some space at the end of the output string.
--
http://mail.python.org/mailman/listinfo/python-list


andyjian430074 at gmail

Nov 23, 2009, 9:11 PM

Post #8 of 9 (485 views)
Permalink
Re: sys.stdout is not flushed [In reply to]

On Nov 23, 8:54 pm, Dave Angel <da...@ieee.org> wrote:
> Jankins wrote:
> > 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.
>
> 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.
--
http://mail.python.org/mailman/listinfo/python-list


nobody at nowhere

Nov 23, 2009, 10:19 PM

Post #9 of 9 (487 views)
Permalink
Re: sys.stdout is not flushed [In reply 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.

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.