Re: Problems with sys.stout.flush()



Carl Banks wrote:
On May 22, 10:33 pm, Joel Ross <jo...@xxxxxxxxxx> wrote:
Hi all,

I'm using python 2.5 and trying to flush the sys.stout buffer with
sys.stout.flush(), but doesn't seem to work. Each time a line is printed
it appends the one before it I need to clear the output and write a
new output without appending the previous one.

That's not how streams work, chief. Once you output (and flush)
something you can't un-output it.

What you probably want to do is to write a carriage return ("\r")
which usually causes the cursor to return to the beginning of the
line, so that any new text you write overwrites the old text.

This has nothing to do with flushing; flushing doesn't erase or clear
the old input. Flushing is usually needed for a different reason,
however, namely standard output doesn't actually get sent to the
console until it sees a newline ("\n") unless you flush the buffer.

Try to adapt this example to your problem:

for i in xrange(11):
sys.stdout.write('*'*(10-i) + ' '*i + '\r')
sys.stdout.flush()
time.sleep(2)

Your example prints a new line each time, Doesn't help me with a progress bar, do you know of anyway doing it with the print command? My progress bars works fine this is the only problem im having with it at the moment. any help would be appreciated.

cheers

Notice that you have to print out spaces as well, to overwrite the
previous characters on the line (well, it might not be strictly needed
for a progress bar that only grows, but it's normally a good idea.


I have tried the -u
(unbuffered) option for python and a few examples from this sitehttp://stackoverflow.com/questions/107705/python-output-buffering

Code:

import sys
from time import sleep

def progress(number, total, char):

percentage = float(number*100)/total
percentage = int(round(percentage))
percentage = int(100 - percentage)
if percentage > 0:
char = char * percentage
sys.stdout.write(char)
sys.stdout.flush() #Not working correctly
sleep(2)

progress(40, 50, "*")
progress(30, 50, "*")
progress(20, 50, "*")
progress(10, 50, "*")
progress(2, 50, "*")

Regards

jross



Carl Banks
.



Relevant Pages

  • Re: Problems with sys.stout.flush()
    ... Once you output (and flush) ... console until it sees a newline unless you flush the buffer. ... option for python and a few examples from this sitehttp://stackoverflow.com/questions/107705/python-output-buffering ...
    (comp.lang.python)
  • Re: "secure" file flag?
    ... you really need to flush the on-device cache on each ... > pass to make sure the bit patterns get written to the platter in proper ... A simple algorithm could just mark each buffer with a special ... read all file blocks into buffers that are marked dirty and get the ...
    (freebsd-hackers)
  • Re: Response.Flush: Differences between IIS 6.0 and 5.0?
    ... since IIS 6.0 is now on top of http.sys. ... smaller buffer and so on, each flush cause the packet to send.... ... > Server: Microsoft-IIS/5.0 ...
    (microsoft.public.inetserver.iis)
  • Re: Can anyone explain why this is happening?
    ... Is it possible that your writes do not include a local buffer flush ... written file (to guarantee that newly written data have been flushed to ... before it reads from a new file (which also flushes all buffered written ...
    (comp.parallel.mpi)
  • Fixing socket.makefile()
    ... Python library's socket.py. ... characters from the socket. ... its own local buffer. ...
    (comp.lang.python)