'Waiting' animation in command prompt (Python) 'Waiting' animation in command prompt (Python) python python

'Waiting' animation in command prompt (Python)


Use \r and print-without-newline (that is, suffix with a comma):

animation = "|/-\\"idx = 0while thing_not_complete():    print(animation[idx % len(animation)], end="\r")    idx += 1    time.sleep(0.1)

For Python 2, use this print syntax:

print animation[idx % len(animation)] + "\r",


Just another pretty variant

import timebar = [    " [=     ]",    " [ =    ]",    " [  =   ]",    " [   =  ]",    " [    = ]",    " [     =]",    " [    = ]",    " [   =  ]",    " [  =   ]",    " [ =    ]",]i = 0while True:    print(bar[i % len(bar)], end="\r")    time.sleep(.2)    i += 1


A loading bar useful for if you're installing something.

animation = ["[        ]","[=       ]","[===     ]","[====    ]","[=====   ]","[======  ]","[======= ]","[========]","[ =======]","[  ======]","[   =====]","[    ====]","[     ===]","[      ==]","[       =]","[        ]","[        ]"]notcomplete = Truei = 0while notcomplete:    print(animation[i % len(animation)], end='\r')    time.sleep(.1)    i += 1

if you want to make it last a couple seconds do

if i == 17*10:    break

after the

i += 1