Python Progress Bar

后端 未结 30 2843
礼貌的吻别
礼貌的吻别 2020-11-22 06:13

How do I use a progress bar when my script is doing some task that is likely to take time?

For example, a function which takes some time to complete and returns

30条回答
  •  忘掉有多难
    2020-11-22 07:10

    This is my simple solution:

    import time
    
    def progress(_cur, _max):
        p = round(100*_cur/_max)
        b = f"Progress: {p}% - ["+"."*int(p/5)+" "*(20-int(p/5))+"]"
        print(b, end="\r")
    
    # USAGE:
    for i in range(0,101):
        time.sleep(0.1) 
        progress(i,100)
    
    print("..."*5, end="\r")
    print("Done")
    

提交回复
热议问题