from __future__ import print_function
import os.path as osp
from six.moves import urllib
from .makedirs import makedirs
[docs]def download_url(url, folder, name=None, log=True):
r"""Downloads the content of an URL to a specific folder.
Args:
url (string): The url.
folder (string): The folder.
log (bool, optional): If :obj:`False`, will not print anything to the
console. (default: :obj:`True`)
"""
data = urllib.request.urlopen(url)
if name is None:
filename = url.rpartition("/")[2]
else:
filename = name
path = osp.join(folder, filename)
if osp.exists(path): # pragma: no cover
if log:
print("Using exist file", filename)
return path
if log:
print("Downloading", url)
makedirs(folder)
data = urllib.request.urlopen(url)
with open(path, "wb") as f:
f.write(data.read())
return path