Created
April 17, 2020 05:53
-
-
Save ArmaanMcleod/fa72ad354b1a997462679e11e5733638 to your computer and use it in GitHub Desktop.
Basic function for downloading file over HTTP
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
from pathlib import Path | |
from requests import get | |
from requests.exceptions import HTTPError | |
def download_file(url, chunk_size=1024): | |
filename = Path(url).name | |
if not Path(filename).exists(): | |
try: | |
with get(url, stream=True) as req: | |
req.raise_for_status() | |
with open(filename, mode="wb") as f: | |
for chunk in req.iter_content(chunk_size=chunk_size): | |
if chunk: | |
f.write(chunk) | |
except HTTPError as ex: | |
print(ex) | |
return None | |
return filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment