进度条打印函数

进度条打印:

import time
def progress(per,width=50):
    text = ('
[%%-%ds]'%width)%('x'*int(per*width))
    text += '%3s%%'
    text = text%(round(per*100))
    print(text,end='')

file_size=102400
cur_size=0
while cur_size < file_size:
    cur_size += 2048
    per = cur_size / file_size
    progress(per)
    time.sleep(0.1)

print('

完毕')
# 
代表每次光标回到最左边,%%代表%,这里的负数'-'代表'x'打印在最左边,不然默认在最右边。
# int类型的才能字符串拼接(浮点不行)
# %20s里面的20如果需要格式化输出的话需要二次格式化输出,不能一次。
原文地址:https://www.cnblogs.com/suguangti/p/10802720.html