擅长:python、mysql、java
<p>似乎<a href="http://progressbar-2.readthedocs.io/en/latest/usage.html" rel="nofollow noreferrer">Progress Bar Usage</a>页面上的示例与代码实际需要的内容之间存在脱节。</p>
<p>在下面的示例中,请注意使用<code>maxval</code>,而不是<code>max_value</code>。还要注意使用<code>.start()</code>初始化条。这已经在<a href="https://github.com/niltonvolpato/python-progressbar/issues/36" rel="nofollow noreferrer">Issue</a>中注意到了。</p>
<pre><code>import progressbar
import requests
url = "http://stackoverflow.com/"
def download_file(url):
local_filename = 'test.html'
r = requests.get(url, stream=True)
f = open(local_filename, 'wb')
file_size = int(r.headers['Content-Length'])
chunk = 1
num_bars = file_size / chunk
bar = progressbar.ProgressBar(maxval=num_bars).start()
i = 0
for chunk in r.iter_content():
f.write(chunk)
bar.update(i)
i+=1
f.close()
return
download_file(url)
</code></pre>