Created
January 13, 2017 17:53
-
-
Save qhuang872/6925be135e6b5b629340ed71a883e6bb to your computer and use it in GitHub Desktop.
This file contains 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
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. |
This file contains 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
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