Skip to content

Instantly share code, notes, and snippets.

@ArmaanMcleod
Created April 17, 2020 05:53
Show Gist options
  • Save ArmaanMcleod/fa72ad354b1a997462679e11e5733638 to your computer and use it in GitHub Desktop.
Save ArmaanMcleod/fa72ad354b1a997462679e11e5733638 to your computer and use it in GitHub Desktop.
Basic function for downloading file over HTTP
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