Downloading files with Python and the Requests library


""" 
Adapted from https://github.com/tqdm/tqdm/blob/master/examples/tqdm_requests.py
"""

from os import devnull
import requests
from tqdm.auto import tqdm

def download_file(url,file_path):
    """ Downloads file from url specified in `url` to file path 
        specified with `file_path`
    """
    file_name = url.replace('/', ' ').split()[-1] # last part of URL is file name
    response = requests.get(url, stream=True)
    with open(file_path, "wb") as fout:
        with tqdm(
            unit='B', unit_scale=True, unit_divisor=1024, miniters=1,
            desc=file_name, total=int(response.headers.get('content-length', 0))
        ) as pbar:
            for chunk in response.iter_content(chunk_size=4096):
                fout.write(chunk)
                pbar.update(len(chunk))

Comments

Popular posts from this blog

QTreeView and QTableView dynamic changes

C++ strings and string_view