-1

I'm trying to run a time time-consuming 'for' loop, and want to print the process in the fixed place of the console and refresh each loop

I try print function like this

for i in range(total):
    ...some code....
    print('---%.2f---'%(float(i)/total), sep='')

It seems does't work

  • 2
    Possible duplicate of [Text Progress Bar in the Console](https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console) – Derte Trdelnik May 22 '19 at 10:50

1 Answers1

0

I use this answer to display progress bar:

The methods used:

def startProgress(title):
    global progress_x
    sys.stdout.write(title + ": [" + "-"*50 + "]" + chr(8)*51)
    sys.stdout.flush()
    progress_x = 0


def progress(x):
    global progress_x
    x = int(x * 50 // 100)
    sys.stdout.write("#" * (x - progress_x))
    sys.stdout.flush()
    progress_x = x


def endProgress():
    sys.stdout.write("#" * (50 - progress_x) + "]\n")
    sys.stdout.flush()

An example of use:

import time
import sys

startProgress("Test")
for i in range(500):
    progress(i * 100 / 500)
    time.sleep(.1)
endProgress()

The progress will look like this with the # moving at the same time of the progress:

enter image description here

Alexandre B.
  • 5,387
  • 2
  • 17
  • 40