-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
51 lines (41 loc) · 1.36 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
import os
import pathlib
import requests
from tqdm import tqdm
def download_file(
url: str,
filename: str,
base: str = ".",
dir: str = "data",
overwrite: bool = False,
silent: bool = False,
):
"""Method for downloading a file from the world-wide cyber space
Arguments:
filename {str} -- File name
Keyword Arguments:
base {str} -- Base directory (default: {"."})
dir {str} -- Download directory (default: {"data"})
overwrite {bool} -- If {True} existing files with be overwritten (default: {False})
Returns:
{str} -- Returns a pointer to `filename`.
"""
filepath = os.path.join(base, dir, filename)
if pathlib.Path(filepath).is_file() and not overwrite:
print(f"{filepath} already exist. To overwrite pass `overwrite=True`")
return
chunkSize = 1024
name, _ = os.path.splitext(filename)
r = requests.get(url, stream=True)
with open(filepath, "wb") as f:
if not silent:
pbar = tqdm(
unit="B", unit_scale=True, total=int(r.headers["Content-Length"])
)
for chunk in r.iter_content(chunk_size=chunkSize):
if chunk: # filter out keep-alive new chunks
if not silent:
pbar.update(len(chunk))
f.write(chunk)
return filename