Python Progress Bar

后端 未结 30 2560
礼貌的吻别
礼貌的吻别 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 06:52

    Here's a short solution that builds the loading bar programmatically (you must decide how long you want it).

    import time
    
    n = 33  # or however many loading slots you want to have
    load = 0.01  # artificial loading time!
    loading = '.' * n  # for strings, * is the repeat operator
    
    for i in range(n+1):
        # this loop replaces each dot with a hash!
        print('\r%s Loading at %3d percent!' % (loading, i*100/n), end='')
        loading = loading[:i] + '#' + loading[i+1:]
        time.sleep(load)
    

提交回复
热议问题