Search code examples
pythonpython-requests

Progress Bar while download file over http with Requests


I need to download a sizable (~200MB) file. I figured out how to download and save the file with here. It would be nice to have a progress bar to know how much has been downloaded. I found ProgressBar but I'm not sure how to incorporate the two together.

Here's the code I tried, but it didn't work.

bar = progressbar.ProgressBar(max_value=progressbar.UnknownLength)
with closing(download_file()) as r:
    for i in range(20):
        bar.update(i)

Solution

  • I suggest you try tqdm, it's very easy to use. Example code for downloading with requests library:

    from tqdm import tqdm
    import requests
    
    url = "http://www.ovh.net/files/10Mb.dat"
    filepath = "test.dat"
    
    # Streaming, so we can iterate over the response.
    response = requests.get(url, stream=True)
    
    # Sizes in bytes.
    total_size = int(response.headers.get("content-length", 0))
    block_size = 1024
    
    with tqdm(total=total_size, unit="B", unit_scale=True) as progress_bar:
        with open(filepath, "wb") as file:
            for data in response.iter_content(block_size):
                progress_bar.update(len(data))
                file.write(data)
    
    if total_size != 0 and progress_bar.n != total_size:
        raise RuntimeError("Could not download file")