在Python中,你可以使用sys
库来实现打印进度条。以下是一个简单的例子:
import sys
import time
def print_progress_bar(iteration, total, prefix='', suffix='', length=50, fill='█'):
percent = ("{0:.1f}").format(100 * (iteration / float(total)))
filled_length = int(length * iteration // total)
bar = fill * filled_length + '-' * (length - filled_length)
sys.stdout.write('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix))
sys.stdout.flush()
# 示例:打印一个进度条
total = 100
for i in range(total + 1):
print_progress_bar(i, total, prefix='Progress:', suffix='Complete', length=50)
time.sleep(0.1)
运行以上代码,你将看到一个简单的进度条在命令行中打印出来。你可以根据自己的需求调整进度条的长度、填充字符等参数。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python打印进度条的方法是什么