Python Progress Bar

后端 未结 30 2608
礼貌的吻别
礼貌的吻别 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:59

    I used format() method to make a load bar. Here is my solution:

    import time
    
    loadbarwidth = 23
    
    for i in range(1, loadbarwidth + 1):
        time.sleep(0.1) 
    
        strbarwidth = '[{}{}] - {}\r'.format(
            (i * '#'),
            ((loadbarwidth - i) * '-'),
            (('{:0.2f}'.format(((i) * (100/loadbarwidth))) + '%'))
        )
    
        print(strbarwidth ,end = '')
    
    print()
    

    Output:

    [#######################] - 100.00%
    

提交回复
热议问题