help with printing to stdout...

Lie Ryan lie.1296 at gmail.com
Sun Mar 8 08:45:40 EDT 2009


Chris Rebert wrote:
> On Sun, Mar 8, 2009 at 1:37 AM, Daniel Dalton <d.dalton at iinet.net.au> wrote:
>> Hi,
>>
>> 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
> 
> Use the carriage return character to overwrite the line (you'll need
> to forego `print`):
> 
> from sys import stdout
> while working:
>     stdout.write('\r'+percent)
> 
> Note that you'll need to ensure that `percent` has constant length
> throughout the loop.
> 

or "erase" the previous character first with whitespace
stdout.write('\r    \r%s%%' % percent).

curse might be more reliable for this kind of thing (though it is 
definitely an overkill)

Note: \r doesn't work on IDLE



More information about the Python-list mailing list