Skip to content

Instantly share code, notes, and snippets.

@tvst
Created June 20, 2020 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvst/b33a1a75f715b4b59407fc8449d9404b to your computer and use it in GitHub Desktop.
Save tvst/b33a1a75f715b4b59407fc8449d9404b to your computer and use it in GitHub Desktop.
A simple file downloader for Python
import os
import requests
def download_file(url, folder, filename):
"""Downloads a file from the internet, but only if it doesn't already exist on disk.
Parameters
----------
url : str
The URL to download.
folder : list or tuple
The folder where to store the file, as list.
For example, use [".", "files", "model"] instead of "./files/model/".
filename : str
The name of the file on disk.
Example
-------
>>> download_file(
... 'https://upload.wikimedia.org/wikipedia/commons/0/0e/Tree_example_VIS.jpg',
... ('.', 'foo', 'bar'),
... 'example.jpg')
"""
full_filepath = os.path.abspath(os.path.expanduser(os.path.expandvars(
os.path.join(*tuple(folder)))))
full_filename = os.path.join(full_filepath, filename)
if os.path.isfile(full_filename):
return
os.makedirs(full_filepath, exist_ok=True)
resp = requests.get(url, stream=True)
with open(full_filename, 'wb') as file_desc:
for chunk in resp.iter_content(chunk_size=5000000):
file_desc.write(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment