help with printing to stdout...

Hendrik van Rooyen mail at microcorp.co.za
Sun Mar 8 08:48:31 EDT 2009


"Daniel Dalton" <d.dalton at iinet.net.au> wrote:

> I've got a program here that prints out a percentage of it's
> completion. Currently with my implimentation it prints like this:
> 0%
> 1%
> 2%
> 3%
> 4%
> 
> etc taking up lots and lots of lines of output... So, how can I make it
> write the percentage on the same line eg. 
> while working:
>   print percent
> every time the line print percent is ran it should delete the old
> percentage from the screen, replacing it with the new one, so as to only
> use up one line... Basically I'm just printing a string of text to the
> screen and every time my print command is ran I would like the old text
> to be removed and my new text added (talking about the one line of the
> screen here)... This is a command line program, under linux...

Play with the following:

put a comma after the print, like this:

print percent,    #This keeps it on the same line

Then put a carriage return at the start, like this:

print '\r',percent,

or like this:

print '\r'+str(percent),

Then make sure it gets sent out, like this:

sys.stdout.flush()

Alternatively, you can play with backspaces instead of the carriage return:

print '\b\b\b\b',
print percent,
sys.stdout.flush()

And see what happens.

- Hendrik





More information about the Python-list mailing list