Skip to content

Instantly share code, notes, and snippets.

@qhuang872
Created January 13, 2017 17:38
Show Gist options
  • Save qhuang872/4bbdff079edfefa0b7e45ec1156a7598 to your computer and use it in GitHub Desktop.
Save qhuang872/4bbdff079edfefa0b7e45ec1156a7598 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"""
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