简体   繁体   中英

Python: Carriage return stops working after 10000th time

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM