用python实现动态进度条(两种形式)
#动态进度条 #第一种 import time scale = 50 #50表示进度条的长度 print("开始".center(scale //2, "-")) #居中对齐center #左对齐为ljust #右对齐为rjust #默认左对齐format start = time.perf_counter()#起始时间 for i in range(scale+1): z = >* i b = - * (scale-i) h = (i/scale)*100 dur = time.perf_counter() - start print(" {:^3.0f}%[{}->{}{:.4f}s]".format(h, z, b, dur), end= ) #{:^3.0f}%[{}->{}],表示10%->.......... #{:.4f}s 表示0.0001s,即进度条的秒数精确到小数点后四位。 #end= 控制end为空的,使其不用换行。 time.sleep(0.1) #速度 print( +"结束执行".center(scale//2, -))
第一种代码执行如下:
此动态进度条是连续不换行的。
#动态进度条 #第二种 import time scale = 50 print("开始".center(scale //2, "-"))#左对齐 start = time.perf_counter()#获取起始时间 for i in range(scale+1): z = ** i b = - * (scale-i) h = (i/scale)*20 dur = time.perf_counter() - start print(" {:^3.0f}%[{}->{}{:.2f}s]".format(h, z, b, dur)) time.sleep(1) print( +"结束执行".center(scale//2, -))
第二种代码执行如下:
此动态进度条是换行执行的。