Python CLI(命令行)进度条
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python CLI(命令行)进度条相关的知识,希望对你有一定的参考价值。
This is a function that will show a progress bar of the given percentage. Useful when performing time consuming tasks.
import sys import time import math # Output example: [======= ] 75% # width defines bar width # percent defines current percentage def progress(width, percent): marks = math.floor(width * (percent / 100.0)) spaces = math.floor(width - marks) loader = '[' + ('=' * int(marks)) + (' ' * int(spaces)) + ']' sys.stdout.write("%s %d%% " % (loader, percent)) if percent >= 100: sys.stdout.write(" ") sys.stdout.flush() # Simulate doing something... for i in xrange(100): progress(50, (i + 1)) # +1 because xrange is only 99 time.sleep(0.1) # Slow it down for demo
以上是关于Python CLI(命令行)进度条的主要内容,如果未能解决你的问题,请参考以下文章