sys.stdout.write()输出不会自动换行,没有end,可用转义字符自行控制 /n 换行 /r 回车到本行首,可刷新输出 如用sys.stdout.write() 和\r实现自定义进度条
import time import sys def main(): j = 0 for i in range(100): j+=1 sys.stdout.write(‘\r已经上传了%%%d‘ % (j/100*100)) time.sleep(0.1) if __name__ == ‘__main__‘: main()
结果:
已经上传了%100
import time import sys for i in range(100): a = int((i + 1) / 5) b = 20 - a # sys.stdout.write(‘\r>>convert image %d/%d‘%(i,b)) sys.stdout.write(‘\r|%s%s|%d%%‘ % (a * ‘▇‘, b * ‘ ‘, i + 1)) sys.stdout.flush() time.sleep(.1)
结果:
|▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇|100%
from tqdm import tqdm import time import sys for i in tqdm(range(1000)): time.sleep(0.01)
结果:
100%|██████████| 1000/1000 [00:10<00:00, 91.47it/s]
原文:https://www.cnblogs.com/-wenli/p/10887454.html