Python Progress Bar

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

    If it is a big loop with a fixed amount of iterations that is taking a lot of time you can use this function I made. Each iteration of loop adds progress. Where count is the current iteration of the loop, total is the value you are looping to and size(int) is how big you want the bar in increments of 10 i.e. (size 1 =10 chars, size 2 =20 chars)

    import sys
    def loadingBar(count,total,size):
        percent = float(count)/float(total)*100
        sys.stdout.write("\r" + str(int(count)).rjust(3,'0')+"/"+str(int(total)).rjust(3,'0') + ' [' + '='*int(percent/10)*size + ' '*(10-int(percent/10))*size + ']')
    

    example:

    for i in range(0,100):
         loadingBar(i,100,2)
         #do some code 
    

    output:

    i = 50
    >> 050/100 [==========          ]
    

提交回复
热议问题