[Tutor] Writing to the terminal?

Terry Carroll carroll at tjc.com
Sun Dec 12 21:44:21 CET 2010


On Fri, 10 Dec 2010, Modulok wrote:

> Assume I'm working in a command shell on a terminal. Something like
> tcsh on xterm, for example. I have a program which does *something*.
> Let's say it counts down from 10. How do I print a value, and then
> erase that value, replacing it with another value? Say I had something
> like '10' that appears, then wait a second, then the 10 is replaced by
> '9'... '8'.. and so forth.


import time
for t in range(10,0, -1):
     print "%s \x0D" %t,
     time.sleep(1)
print # get to next line
print "Done!"


The magic is \0x0D, which resets to the front of the line; and the 
trailing comma, which suppresses starting the next print on a new line.


More information about the Tutor mailing list