本文實例講述了python下載文件時顯示下載進度的方法。分享給大家供大家參考。具體分析如下:
將這段代碼放入你的腳本中,類似:urllib.urlretrieve(getFile, saveFile, reporthook=report)
第三個參數如下面的函數定義report,urlretrieve下載文件時會實時回調report函數,顯示下載進度
def report(count, blockSize, totalSize): percent = int(count*blockSize*100/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()希望本文所述對大家的Python程序設計有所幫助。
新聞熱點
疑難解答