0

I found a way to make a progress bars and have been using this to visualize how far along the script has gotten. I used it for each loop to start with but it quickly became messy, so I updated it to work for the whole file.

When using carriage return in Python, after 10,000th time trying to overwrite the same line it starts ignoring carriage return and just prints a new line each time.

This code has been working in the past, but when switching to working for the whole file in one bar it breaks.

def ProgressBar (iteration, total, tot_len = 50, fill = '█', empty = '-'):
    iteration+=1 #to fix the OBO issue
    percents = round(100.0*iteration/float(total),1)
    filled_len = int(round(tot_len * percents/100.0))
    bar = fill * filled_len + empty * (tot_len - filled_len)
    sys.stdout.write(' |%s| %s%s (%s/%s) \r' % (bar, percents, '%', iteration, total))
    sys.stdout.flush()
    if iteration == total:
        print ' '

I would expect this to rewrite over the same line continuously regardless of the number of times the function is called but after 10000 calls in no longer rewrites over the same line.

I end up getting this out

|████----------------------------------------------| 8.3% (10000/120000) 
|████----------------------------------------------| 8.3% (10001/120000) 
|████----------------------------------------------| 8.3% (10002/120000) 
|████----------------------------------------------| 8.3% (10003/120000) 
|████----------------------------------------------| 8.3% (10004/120000) 
|████----------------------------------------------| 8.3% (10005/120000) 
|████----------------------------------------------| 8.3% (10006/120000) 
|████----------------------------------------------| 8.3% (10007/120000) 
|████----------------------------------------------| 8.3% (10008/120000) 
|████----------------------------------------------| 8.3% (10009/120000) 
|████----------------------------------------------| 8.3% (10010/120000) 
|████----------------------------------------------| 8.3% (10011/120000) 
|████----------------------------------------------| 8.3% (10012/120000) 
|████----------------------------------------------| 8.3% (10013/120000) 
|████----------------------------------------------| 8.3% (10014/120000) 
|████----------------------------------------------| 8.3% (10015/120000) 

and it just keeps going.

Chris
  • 1
  • I would guess that reaching 10000 finally caused your line of text to entirely fill a line of your terminal, so the cursor was wrapping to the next line. At the start of a line, `\r` does nothing. Reducing `tot_len` would fix that - ideally it would be derived from the actual width of your terminal, but that makes things a lot more complicated. – jasonharper May 13 '19 at 18:46
  • Yup, I feel real dumb now. Thanks! – Chris May 13 '19 at 18:48

1 Answers1

0

At some point, incrementing a number will increase its length to the point where one line will not fit on the terminal line. If 10,000 is that limit, you'll need to augment tot_len accordingly.

Alec
  • 8,529
  • 8
  • 37
  • 63