Method that shows the download progress when python downloads a file

  • 2020-05-05 11:24:55
  • OfStack

This article illustrates an example of how to display the download progress when python downloads a file. Share with you for your reference. The specific analysis is as follows:

Put this code into your script like urllib.urlretrieve (getFile, saveFile, reporthook=report)

The third parameter defines report in the following function. When urlretrieve downloads a file, it will call back report function in real time to show the download progress of


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()

I hope this article has been helpful to your Python programming.


Related articles: