python下載文件時顯示下載進度的代碼

pythopen 9年前發布 | 3K 次閱讀 Python

將這段代碼放入你的腳本中,類似:urllib.urlretrieve(getFile, saveFile, reporthook=report)
第三個參數如下面的函數定義report,urlretrieve下載文件時會實時回調report函數,顯示下載進度

def report(count, blockSize, totalSize):
    percent = int(countblockSize100/totalSize)
    sys.stdout.write("\r%d%%" % percent + ' complete')
    sys.stdout.flush()

sys.stdout.write('\rFetching ' + name + '...\n') urllib.urlretrieve(getFile, saveFile, reporthook=report) sys.stdout.write("\rDownload complete, saved as %s" % (fileName) + '\n\n') sys.stdout.flush()</pre>

 本文由用戶 pythopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!