0

So im really not sure if this is possible but im trying to create a progress bar for my code in python but for each iteation i gt a new print line but what i want is the console to utilise the first statement...

progress = []
noDots = 50
noItems =601
if noItems >= noDots:
    batch = int(noItems/noDots)
else:
    noDots = noItems
    batch = 1
for i in range(noDots):
    progress.insert(0, '.')
for i in range(0,noItems,batch):
    val = int(i/batch)
    perc = round(((i/noItems)*100))
    print(val)
    if val ==0:
        val == 0
    else:
        val -= 1
    if progress[val] == '.':
        progress[val] = '>'
        if val == 0:
            continue
        else:
            progress[val - 1] = '='
        print(f'[{"".join(progress)}] {perc}%')

The output is as follows...

Heres the output...

Clio Tech
  • 61
  • 10
  • Does this answer your question? [Print to the same line and not a new line in python](https://stackoverflow.com/questions/3419984/print-to-the-same-line-and-not-a-new-line-in-python). Basically you just need to add `'\r'` at the start of the `print` – Tomerikoo May 11 '20 at 16:43

1 Answers1

0

If you use sys.stdout you should be able to do it

# To see how far we're in the progress
string = '-'
for i in range(x): 
    sys.stdout.write('\r' + '(' + string + ' '*(max_len - len(string)  + ')')
    sys.stdout.flush()
    string += '-'

It's important to have the '\r' character

Marius Johan
  • 394
  • 3
  • 13
  • 1
    it's also maybe more important to `flush`. I believe this can be done with `print` just the same (`print` has a `flush=True` argument) – Tomerikoo May 11 '20 at 17:02