擅长:python、mysql、java
<p>有一些特定的库(<a href="http://pypi.python.org/pypi/progressbar2" rel="noreferrer">like this one here</a>),但也许一些非常简单的方法可以做到:</p>
<pre><code>import time
import sys
toolbar_width = 40
# setup toolbar
sys.stdout.write("[%s]" % (" " * toolbar_width))
sys.stdout.flush()
sys.stdout.write("\b" * (toolbar_width+1)) # return to start of line, after '['
for i in xrange(toolbar_width):
time.sleep(0.1) # do real work here
# update the bar
sys.stdout.write("-")
sys.stdout.flush()
sys.stdout.write("]\n") # this ends the progress bar
</code></pre>
<p>注:<a href="https://pypi.org/project/progressbar2/" rel="noreferrer">progressbar2</a>是<a href="https://pypi.python.org/pypi/progressbar" rel="noreferrer">progressbar</a>的叉子,多年来没有保养过。</p>