Remove and Replace Printed items

Alex  picture Alex · Mar 13, 2011 · Viewed 204.4k times · Source

I was wondering if it was possible to remove items you have printed in Python - not from the Python GUI, but from the command prompt. e.g.

a = 0  
for x in range (0,3):  
    a = a + 1  
    b = ("Loading" + "." * a)
print (a)

so it prints

>>>Loading   
>>>Loading. 
>>>Loading.. 
>>>Loading...

But, my problem is I want this all on one line, and for it it remove it self when something else comes along. So instead of printing "Loading", "Loading.", "Loading... I want it to print "Loading.", then it removes what is on the line and replaces it with "Loading.." and then removes "Loading.." and replaces it (on the same line) with "Loading...". It's kind of hard to describe.

p.s I have tried to use the Backspace character but it doesn't seem to work ("\b")

Thanks

Answer

Keith picture Keith · Mar 13, 2011

Just use CR to go to beginning of the line.

import time
for x in range (0,5):  
    b = "Loading" + "." * x
    print (b, end="\r")
    time.sleep(1)