1.0 Code:
Import time # introduction time libraries Scale = 10 # text Scrubber width Print ( " ------ execution start ------ " ) for I in Range (+ Scale. 1): # simulate a progress A = ' * ' * I # number string is copied, "*" indicates the percentage of information expressed by B = ' . ' * (scale- I) C = (I / Scale) * 100 # outputs a corresponding percentage progress bar Print ( " {:} ^ 3.0f% [{} -> {}] " .format (C, A, B)) the time.sleep ( 0.1) # interval same time executives Print ( " ------ End ------ execution " )
result:
2.0 Code (single dynamic refresh):
Code (IDLE may not run, I was running in the Visual Studio 2019):
Import time # introduction time libraries for I in Range (101 ): Print ( " \ R & lt {:. 3}% " .format (I), End = "" ) # "\ R & lt" the cursor to withdraw the current row of the line , "end =" function so that the output does not print wrap " the time.sleep (0.1)
Results: Output from 0% to 100%
Complete results:
Code:
import time#引入time库 scale=50#文本进度条宽度 print("执行开始".center(scale//2,"-")) start=time.perf_counter()#计时开始 for i in range(scale+1):#模拟一个进度 a='*'*i#字符串被复制的次数,"*"表示百分比所表达的信息 b='.'*(scale-i) c=(i/scale)*100#输出对应进度条的百分比 dur=time.perf_counter()-start#计时结束,并计算所用时间 print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end="")#dur用来记录打印文本进度条所消耗的时间 time.sleep(0.1)#间隔相同时间执行程序 print("\n"+"执行结束".center(scale//2,"-"))
结果(Visual Studio 2019):