The '
'
character (carriage return) resets the cursor to the beginning of the line and allows you to write over what was previously on the line.
from time import sleep
import sys
for i in range(21):
sys.stdout.write('
')
# the exact output you're looking for:
sys.stdout.write("[%-20s] %d%%" % ('='*i, 5*i))
sys.stdout.flush()
sleep(0.25)
I'm not 100% sure if this is completely portable across all systems, but it works on Linux and OSX at the least.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…