方法六:自定义图形进度条
示例代码
import time
import sys
def custom_progress_bar(iteration, total, length=40):
percent = (iteration / total) * 100
filled_length = int(length * iteration // total)
bar = '#' * filled_length + '-' * (length - filled_length)
sys.stdout.write(f'\r[{bar}] {percent:.2f}%')
sys.stdout.flush()
# 示例用法
total_steps = 100
for i in range(total_steps + 1):
custom_progress_bar(i, total_steps)
time.sleep(0.1)
print() # 换行
解释
#符号来表示已完成的部分,-符号表示未完成的部分,最终输出百分比。