python文本进度条

设计一款文本进度条,前50%的进度很快达到,后面在进度相对慢些

实现起来很容易的,在后期适当加入个sleep函数就行了

import sys
import time

def progress_bar(percent):
    bar_length = 20
    if percent > 100:
        percent = 100
    if percent < 0:
        percent = 0
    hashes = '#' * int(percent/100.0 * bar_length)
    spaces = ' ' * (bar_length - len(hashes))
    sys.stdout.write("\rProgress: [{0}] {1}%".format(hashes + spaces, percent))
    sys.stdout.flush()

    if percent <= 50:
        time.sleep(0.1)
    else:
        time.sleep(0.5)