\r functionality

J

Jake

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?
 
M

Markus Weihs

Hi!
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?

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
 
?

=?ISO-8859-1?Q?Daniel_Sch=FCle?=

Jake said:
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?
 
D

Duncan Booth

Jake said:
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?
.... 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).
 
F

Fredrik Lundh

Jake said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top