参考: , and what is the difference between them? [duplicate]
参考:python的print格式化输出,以及使用format来控制。
实现思路就是不停地删除之前打印的内容,通过 ' ' 实现光标返回最前,之后会覆盖内容,没被覆盖的还会继续显示。
import sys,time # 变量 total = 153 for i in range(total): if i+1 == total: percent = 100.0 print('Progress: %s [%d/%d]'%(str(percent)+'%',i+1,total),end=' ') else: percent = round(1.0 * i / total * 100,2) print('Progress: %s [%d/%d]'%(str(percent)+'%',i+1,total),end=' ') time.sleep(0.01)
import sys,time # 变量 total = 153 for i in range(total): if i+1 == total: percent = 100.0 print('Progress: '+str(percent)+'% ['+str(i+1)+'/'+str(total)+']', end=' ') else: percent = round(1.0 * i / total * 100,2) print('Progress: '+str(percent)+'% ['+str(i+1)+'/'+str(total)+']', end=' ') time.sleep(0.01)