Python

Stream-Download Large File with Progress

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Download a file in chunks so memory stays flat regardless of file size, with a tqdm progress bar showing speed + ETA. Skips re-download if the file is already complete.
Python
Raw
import requests
from pathlib import Path
from tqdm import tqdm

def download(url: str, dest: str | Path, chunk: int = 1 << 16) -> Path:
    dest = Path(dest)
    with requests.get(url, stream=True, timeout=30) as r:
        r.raise_for_status()
        total = int(r.headers.get("content-length") or 0)
        if dest.exists() and dest.stat().st_size == total:
            return dest                       # already complete
        with dest.open("wb") as f, tqdm(
            total=total, unit="B", unit_scale=True, desc=dest.name
        ) as bar:
            for piece in r.iter_content(chunk_size=chunk):
                f.write(piece)
                bar.update(len(piece))
    return dest

download("https://example.com/big-file.zip", "downloads/big-file.zip")
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.