Download file with requests
To download the file from web with requests module is the easiest, you just need to turn on stream while using get method. Then download the file chunk by chunk by using the iter_content method.
Here is the example code on how to download file with requests.
import requests from pathlib import Path import os url = "https://www.python.org/ftp/python/3.7.9/python-3.7.9-macosx10.9.pkg" filename = os.path.basename(url) dl_path = os.path.join(Path.home(), "tmp") os.makedirs(dl_path, exist_ok=True) abs_path = os.path.join(dl_path, filename) with requests.get(url, stream=True) as r, open(abs_path, "wb") as f: for chunk in r.iter_content(chunk_size=1024): f.write(chunk)
Download file with progress bar
If you are downloading a huge file such as 500MB or 1GB or more, you may want to include a progress bar so that you are updated on the file download progress instead of waiting on a blank screen while running the script. You can write your own logic, but an easier way is to install tqdm which is easy to use, tqdm helps you track the progress of your file download.
You can find my script with documentation here.
This is how the script will be like: