[Tutor] Writing to the terminal?

Emile van Sebille emile at fenx.com
Sat Dec 11 01:36:13 CET 2010


On 12/10/2010 12:14 PM Modulok said...
> List,
>
> Forgive me if I don't describe this well, I'm new to it:
>
> 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. The point is, I don't want to print to a
> new line, nor do I want the new number to appear next to the previous
> number... I just want to change it in place. (If that makes any
> sense?) Think of console based progress counters in programs like
> fetch or wget, or lame.
>
> How do you do this in Python?
> -Modulok-
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

The trick is starting python with the -u option:

emile at paj39:~$ python -u
Python 2.6.4rc2 (r264rc2:75497, Oct 20 2009, 02:55:11)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import time
 >>> for ii in range(10):
...     print "   %s" % (ii,),
...     time.sleep(2)
...     print "\r",
...
 >>>

Emile



More information about the Tutor mailing list