Printing to console, no scroll

Harry George harry.g.george at boeing.com
Wed Jan 14 12:50:30 CET 2004


"Totte Karlsson" <mtk at qm.com> writes:

> Hi,
> How can I print to the console without having it scrolling to a new line for
> each print statement?
> I want to print a count down in the console, but for each count it scrolls
> the screen (of course).
> 
>  Is there another way?
> 
> Here is the simple script for now
> 
> print "Closing window in :"
> for second in range(10):
>     time.sleep(1)
>     print `10-second` +" seconds"
> 
> thanks
> /totte
> 
> 
> 
> 

You need to flush after the print statements.  If you have several
prints (not just 1 as in this example), it is easier to hide in a
separate function.

def msg(txt):
    sys.stdout.write(txt)
    sys.stdout.flush()

> for second in range(10):
>     time.sleep(1)
>     msg(`10-second` +" seconds ")  #add a space at the end to delimit
> print    #to finish off the line


-- 
harry.g.george at boeing.com
6-6M31 Knowledge Management
Phone: (425) 342-5601



More information about the Python-list mailing list