Download files with progress in Python
This is a coding tip article. I will show you how to download files with progress in Python.
The sauce here is to make use of the wget module.
Original article: https://blog.petehouston.com/download-files-with-progress-in-python/
First, install the module into the environment.
$ pip install wget
The wget module is pretty straight-forward, only one function to access, wget.download()
.
Let say we want to download this file http://download.geonames.org/export/zip/US.zip, the implementation will be following:
import wgetwget.download('http://download.geonames.org/export/zip/US.zip')
The output will look like this:
11% [........ ] 73728 / 633847
As you can see, it prints out the progress bar for us to know the progress of downloading, with current bytes retrieved with total bytes.
The second parameter is to set output filename or directory for output file.
There is another parameter, bar=callback(current, total, width=80)
. This is to define how the progress bar is rendered to output screen.
There are two options provided:
- bar_thermometer : show minimum progress
[ ..... ]
- bar_adaptive : (default) provide more info while downloading. You can see above result.
We can also write our own progress bar callback.
Let say we only want to have this format for download progress:
Downloading: 12% [12000 / 100000] bytes
The code will look like this:
def bar_custom(current, total, width=80):
print("Downloading: %d%% [%d / %d] bytes" % (current / total * 100, current, total))
wget.download('http://download.geonames.org/export/zip/US.zip', bar=bar_custom)
and…yeah, it works as expected.
Hi there, I’m a software engineer. I write blog daily at https://blog.petehouston.com/ about technical stuffs and programming.