import sys
import time
for i in range(100):
print('*', end='')
sys.stdout.flush()
time.sleep(.5)
sys.stdout 缓冲默认在换行时刷新,这里需要手动刷新
当然有更好的方式,使用tqdm: python进度条库
tqdm means “progress” in Arabic (taqadum, تقدّم) and is an abbreviation for “I love you so much” in Spanish (te quiero demasiado)
import time
from tqdm import tqdm
# 接收任意可迭代对象
for i in tqdm(range(1000)):
time.sleep(.01)
53%|██████████████████████████████████████████████▋ | 530/1000 [00:06<00:05, 87.84it/s]