download_file_from_url¶
- pyhelpers.ops.download_file_from_url(url, path_to_file, if_exists='replace', max_retries=5, random_header=True, verbose=False, requests_session_args=None, fake_headers_args=None, **kwargs)[source]¶
Downloads a file from a valid URL.
See also [OPS-DFFU-1] and [OPS-DFFU-2].
- Parameters:
url (str) – Valid URL pointing to a web resource.
path_to_file (str | os.PathLike[str]) – Path where the downloaded file will be saved; it can be either a full path with filename or just a filename, in which case it will be saved in the current working directory.
if_exists (str) – Action to take if the specified file already exists; options include
'replace'
(default - download and replace the existing file) and'pass'
(cancel the download).max_retries (int) – Maximum number of retries in case of download failures; defaults to
5
.random_header (bool) – Whether to use a random user-agent header; defaults to
True
.verbose (bool | int) – Whether to print progress and relevant information to the console; defaults to
False
.requests_session_args (dict | None) – [Optional] Additional parameters for initialising the requests session; defaults to
None
.fake_headers_args (dict | None) – [Optional] Additional parameters for generating fake HTTP headers; defaults to
None
.kwargs – [Optional] Additional parameters for the method requests.Session.get().
Examples:
>>> from pyhelpers.ops import download_file_from_url >>> from pyhelpers.dirs import cd >>> from PIL import Image >>> import os >>> url = 'https://www.python.org/static/community_logos/python-logo-master-v3-TM.png' >>> path_to_img = cd("tests", "images", "ops-download_file_from_url-demo.png") >>> # Check if "python-logo.png" exists at the specified path >>> os.path.exists(path_to_img) False >>> # Download the .png file >>> download_file_from_url(url, path_to_img) >>> # If download is successful, check again: >>> os.path.exists(path_to_img) True >>> img = Image.open(path_to_img) >>> img.show() # as illustrated below
Figure 11 The Python Logo.¶
Note
When
verbose=True
, the function requires tqdm.