In python 3 you can do this to print on the same line:
print('', end='\r')
Especially useful to keep track of the latest update and progress.
I would also recommend tqdmfrom here if one wants to see the progress of a loop. It prints the current iteration and total iterations as a progression bar with an expected time of finishing. Super useful and quick. Works for python2 and python3.
def startprogress(title):"""Creates a progress bar 40 chars long on the console
and moves cursor back to beginning with BS character"""global progress_x
sys.stdout.write(title +": ["+"-"*40+"]"+ chr(8)*41)
sys.stdout.flush()
progress_x =0def progress(x):"""Sets progress bar to a certain percentage x.
Progress is given as whole percentage, i.e. 50% done
is given by x = 50"""global progress_x
x = int(x *40//100)
sys.stdout.write("#"* x +"-"*(40- x)+"]"+ chr(8)*41)
sys.stdout.flush()
progress_x = x
def endprogress():"""End of progress bar;
Write full bar, then move to next line"""
sys.stdout.write("#"*40+"]\n")
sys.stdout.flush()
For anyone who stumbles upon this years later (like I did), I tweaked 6502’s methods a little bit to allow the progress bar to decrease as well as increase. Useful in slightly more cases. Thanks 6502 for a great tool!
Basically, the only difference is that the whole line of #s and -s is written each time progress(x) is called, and the cursor is always returned to the start of the bar.
def startprogress(title):
"""Creates a progress bar 40 chars long on the console
and moves cursor back to beginning with BS character"""
global progress_x
sys.stdout.write(title + ": [" + "-" * 40 + "]" + chr(8) * 41)
sys.stdout.flush()
progress_x = 0
def progress(x):
"""Sets progress bar to a certain percentage x.
Progress is given as whole percentage, i.e. 50% done
is given by x = 50"""
global progress_x
x = int(x * 40 // 100)
sys.stdout.write("#" * x + "-" * (40 - x) + "]" + chr(8) * 41)
sys.stdout.flush()
progress_x = x
def endprogress():
"""End of progress bar;
Write full bar, then move to next line"""
sys.stdout.write("#" * 40 + "]\n")
sys.stdout.flush()
回答 5
如果我不太了解(不确定),则要使用<CR>而不是<LR>?
如果可以的话,只要控制台终端允许这样做(当输出si重定向到文件时,它将中断)。
from __future__ import print_function
print("count x\r", file=sys.stdout, end=" ")
import sys
import time
def print_percent_done(index, total, bar_len=50, title='Please wait'):'''
index is expected to be 0 based index.
0 <= index < total
'''
percent_done =(index+1)/total*100
percent_done = round(percent_done,1)
done = round(percent_done/(100/bar_len))
togo = bar_len-done
done_str ='█'*int(done)
togo_str ='░'*int(togo)print(f'\t⏳{title}: [{done_str}{togo_str}] {percent_done}% done', end='\r')if round(percent_done)==100:print('\t✅')
r =50for i in range(r):
print_percent_done(i,r)
time.sleep(.02)
import sys
import time
def print_percent_done(index, total, bar_len=50, title='Please wait'):
'''
index is expected to be 0 based index.
0 <= index < total
'''
percent_done = (index+1)/total*100
percent_done = round(percent_done, 1)
done = round(percent_done/(100/bar_len))
togo = bar_len-done
done_str = '█'*int(done)
togo_str = '░'*int(togo)
print(f'\t⏳{title}: [{done_str}{togo_str}] {percent_done}% done', end='\r')
if round(percent_done) == 100:
print('\t✅')
r = 50
for i in range(r):
print_percent_done(i,r)
time.sleep(.02)
I also have a version with responsive progress bar depending on the terminal width using shutil.get_terminal_size() if that is of interest.