python small example - dynamic text progress bar

Little things, ordinary examples do not like to write XD

 

How to achieve dynamic refresh a single row, the answer is - covering
but how to achieve it covers the
key is not wrapped and can fall back to the starting position


So we should use \ r The thing is to make the cursor falls back to the initial position of the current row 

Remember not let the wrap


The code

#文本进度条.py
import time
scale = 50
print("执行开始".center(scale, "-"))//居中对齐
start = time.perf_counter()//获取起始时间
for i in range(scale+1):
    a = i*'*'
    b = (scale-i)*'.'
    c = (i/scale)*100
    dur = time.perf_counter()//每次获取当前时间
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end='')//输出百分比,图形进度以及当前所用的时间,控制end为空使得不用换行
    time.sleep(0.1)
print('\n'+"结束执行".center(scale, '-'))

 

 running result:

Published 99 original articles · won praise 21 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43721423/article/details/104050237