import time import sys from tqdm import tqdm def view_bar(num, total): rate = float(num) / float(total) rate_num = int(rate * 100) r = "\r"+("="*(2*rate_num//10)+">")+(" "*(20-2*rate_num//10))+'%d%%' % (rate_num, ) sys.stdout.write(r) sys.stdout.flush() # 刷新缓冲区 def view(): for e in tqdm([0,1, 2, 3, 4, 5, 6, 7, 8, 9]): time.sleep(0.1) if __name__ == '__main__': print("downloading:") #第一种 for i in range(0, 101): time.sleep(0.1) view_bar(i, 100) #第二种 view() print("\ndownload successful\n")