Last active
April 16, 2025 11:52
-
-
Save yanqd0/c13ed29e29432e3cf3e7c38467f42f51 to your computer and use it in GitHub Desktop.
Python requests download file with a tqdm progress bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from tqdm import tqdm | |
def download(url: str, fname: str, chunk_size=1024): | |
resp = requests.get(url, stream=True) | |
total = int(resp.headers.get('content-length', 0)) | |
with open(fname, 'wb') as file, tqdm( | |
desc=fname, | |
total=total, | |
unit='iB', | |
unit_scale=True, | |
unit_divisor=1024, | |
) as bar: | |
for data in resp.iter_content(chunk_size=chunk_size): | |
size = file.write(data) | |
bar.update(size) |
Thanks for helping me.
Thanks !
see my fork for additional options:
- try auto detect file name if left empty
- option to overwrite if file already existed
- chunk_size in MiB
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks very much. It is a great solution.