download_file(remote_url, cache=False, show_progress=True, timeout=None, sources=None, pkgname='astropy', http_headers=None)¶
Downloads a URL and optionally caches the result.
It returns the filename of a file containing the URL’s contents. If
cache=Trueand the file is present in the cache, just returns the filename; if the file had to be downloaded, add it to the cache. If
cache="update"always download and add it to the cache.
The cache is effectively a dictionary mapping URLs to files; by default the file contains the contents of the URL that is its key, but in practice these can be obtained from a mirror (using
sources) or imported from the local filesystem (using
import_download_cache). Regardless, each file is regarded as representing the contents of a particular URL, and this URL should be used to look them up or otherwise manipulate them.
The files in the cache directory are named according to a cryptographic hash of their URLs (currently MD5, so hackers can cause collisions). The modification times on these files normally indicate when they were last downloaded from the Internet.
The URL of the file to download
- cachebool or “update”, optional
Whether to cache the contents of remote URLs. If “update”, always download the remote URL in case there is a new version and store the result in the cache.
- show_progressbool, optional
Whether to display a progress bar during the download (default is
True). Regardless of this setting, the progress bar is only displayed when outputting to a terminal.
- timeoutfloat, optional
The timeout, in seconds. Otherwise, use
astropy.utils.data.Conf.remote_timeout. Set this to zero to prevent any attempt to download anything.
- sourceslist of str, optional
If provided, a list of URLs to try to obtain the file from. The result will be stored under the original URL. The original URL will not be tried unless it is in this list; this is to prevent long waits for a primary server that is known to be inaccessible at the moment. If an empty list is passed, then
download_filewill not attempt to connect to the Internet, that is, if the file is not in the cache a KeyError will be raised.
The package name to use to locate the download cache. i.e. for
pkgname='astropy'the default cache location is
- http_headersdict or None
HTTP request headers to pass into
urlopenif needed. (These headers are ignored if the protocol for the
sourcesentry is not a remote HTTP URL.) In the default case (None), the headers are
Accept: */*, where
some_valueis set by
Returns the local path that the file was download to.
Whenever there’s a problem getting the remote file.
When a file was requested from the cache but is missing and no sources were provided to obtain it from the Internet.
Because this function returns a filename, another process could run
clear_download_cachebefore you actually open the file, leaving you with a filename that no longer points to a usable file.