\r functionality

2 views
Skip to first unread message

Jake

unread,
May 17, 2005, 6:17:28 AM5/17/05
to
in c and c++ there is a useful way to refresh an output line in printf
and cout using \r meta command. So for example in the wget application
the progress of the download is updated on the same output line of the
screen. From an intital investigation python seems to lack this. Is
this correct?

Markus Weihs

unread,
May 17, 2005, 6:30:10 AM5/17/05
to
Hi!

No. Well, I think you can't do it with print, but you can do this:

import sys
import time

signs = "|/-\\"
while 1:
for i in signs:
sys.stdout.write(i+"\r")
sys.stdout.flush()
time.sleep(0.2)


Regards, Markus

Daniel Schüle

unread,
May 17, 2005, 7:06:03 AM5/17/05
to

>>> print "a\rb"
b
>>>

Duncan Booth

unread,
May 17, 2005, 7:52:05 AM5/17/05
to
Jake wrote:

>>> for i in range(10000):
... print '\r',i,'and counting',
...
9999 and counting

(not obvious from the copy/paste of the output is that it also displayed
the intervening numbers as it counted). The important thing is to end each
print statement with a comma (or use sys.stdout.write).

Fredrik Lundh

unread,
May 17, 2005, 10:46:29 AM5/17/05
to pytho...@python.org
"Jake" <jacob...@gmail.com> wrote:

> in c and c++ there is a useful way to refresh an output line in printf
> and cout using \r meta command.

that's a control character that's printed to the terminal by printf/cout,
not a "C or C++ meta command"

to print the same character in Python, use the same character escape:

print "\r", count,

(note that if this works or not depends on the terminal you're sending the
output to. most ordinary terminals handle it just fine, but if you're sending
output to an IDE console window, it may not work as expected)

</F>

Reply all
Reply to author
Forward
0 new messages