Created
July 26, 2017 06:42
-
-
Save chankeypathak/9c5f9714ab2d84b707bb07ff82c25328 to your computer and use it in GitHub Desktop.
Download big files from internet in chunks to avoid out of memory error
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 # just a choice of comfort for me | |
def download(url_address, filename): | |
response = requests.get(url_address, stream=True) | |
response.raise_for_status() | |
with open(filename, "wb") as f: | |
total_length = response.headers.get('content-length') | |
if total_length is None: | |
f.write(response.content) | |
else: | |
total_length = int(total_length) | |
for data in response.iter_content(chunk_size = total_length / 100): | |
f.write(data) |
Author
chankeypathak
commented
Jul 27, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment