Skip to content

Instantly share code, notes, and snippets.

@qhuang872
Created January 13, 2017 17:53
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 qhuang872/6925be135e6b5b629340ed71a883e6bb to your computer and use it in GitHub Desktop.
Save qhuang872/6925be135e6b5b629340ed71a883e6bb to your computer and use it in GitHub Desktop.
Original error happens on line 11, that exit_ok was default to be False which caused the file_exist erorr, I made the change to set it to be True.
Also added a docstring for the download_file function.
import os
import urllib.request
from urllib.parse import urlparse
def download_file(url, cachedir=None, ignorecache=False, filename=None) -> str:
"""download_file would create a directory if not exist, then download the file to directory, it accept url, target directory, whether or not ignore existed file, and filename as arguments, it returns the path of the new directory."""
if not cachedir:
cachedir = os.path.join(os.getcwd(), "build")
os.makedirs(cachedir,exit_ok=True)
if not filename:
filename = os.path.basename(urlparse(url).path)
path = os.path.join(cachedir, filename)
if ignorecache or not os.path.exists(path):
urllib.request.urlretrieve(url, path)
return path
if __name__ == "__main__":
dlpath = download_file("http://pantry.learningequality.org/downloads/ka-lite/0.16/content/contentpacks/en.zip")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment