nanopyx.core.io.downloader

 1import tqdm
 2import os
 3import requests
 4
 5
 6def download(url: str, file_path: str):
 7    """Download a file from a url to a file path.
 8    :param url: url to download from
 9    :type url: str
10    :param file_path: path to save the file to
11    :type file_path: str
12    """
13    if os.path.exists(file_path):
14        raise Warning(f"already exists, no need to download: {file_path}")
15        return
16
17    if not os.path.exists(os.path.split(file_path)[0]):
18        os.mkdir(os.path.split(file_path)[0])
19
20    with open(file_path, "wb") as f:
21        response = requests.get(url, stream=True)
22        total = response.headers.get("content-length")
23
24        if total is None:
25            f.write(response.content)
26        else:
27            downloaded = 0
28            total = int(total)
29            with tqdm.tqdm(total=total, unit="iB", unit_scale=True) as pbar:
30                for data in response.iter_content(chunk_size=max(int(total / 1000), 1024 * 1024)):
31                    downloaded += len(data)
32                    f.write(data)
33                    pbar.update(len(data))
def download(url: str, file_path: str):
 7def download(url: str, file_path: str):
 8    """Download a file from a url to a file path.
 9    :param url: url to download from
10    :type url: str
11    :param file_path: path to save the file to
12    :type file_path: str
13    """
14    if os.path.exists(file_path):
15        raise Warning(f"already exists, no need to download: {file_path}")
16        return
17
18    if not os.path.exists(os.path.split(file_path)[0]):
19        os.mkdir(os.path.split(file_path)[0])
20
21    with open(file_path, "wb") as f:
22        response = requests.get(url, stream=True)
23        total = response.headers.get("content-length")
24
25        if total is None:
26            f.write(response.content)
27        else:
28            downloaded = 0
29            total = int(total)
30            with tqdm.tqdm(total=total, unit="iB", unit_scale=True) as pbar:
31                for data in response.iter_content(chunk_size=max(int(total / 1000), 1024 * 1024)):
32                    downloaded += len(data)
33                    f.write(data)
34                    pbar.update(len(data))

Download a file from a url to a file path.

Parameters
  • url: url to download from
  • file_path: path to save the file to