Python Progress Bar

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

    a little more generic answer of jelde015 (credit to him of course)

    for updating the loading bar manually will be:

    import sys
    from math import *
    
    
    def loadingBar(i, N, size):
        percent = float(i) / float(N)
        sys.stdout.write("\r"
                         + str(int(i)).rjust(3, '0')
                         +"/"
                         +str(int(N)).rjust(3, '0')
                         + ' ['
                         + '='*ceil(percent*size)
                         + ' '*floor((1-percent)*size)
                         + ']')
    

    and calling it by:

    loadingBar(7, 220, 40)
    

    will result:

    007/220 [=                                       ]  
    

    just call it whenever you want with the current i value.

    set the size as the number of chars the bar should be

提交回复
热议问题