-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.py
More file actions
38 lines (31 loc) · 963 Bytes
/
utils.py
File metadata and controls
38 lines (31 loc) · 963 Bytes
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
import io
import os
import tarfile
import zipfile
import requests
from tqdm import tqdm
def download(url, save_filepath, extract_dir=None, file_type=''):
r = requests.get(url, stream=True)
total_size = int(r.headers.get('content-length'))
block_size = 256*1024
pbar = tqdm(r.iter_content(chunk_size=block_size),
total=total_size,
unit='B', unit_scale=True)
with open(save_filepath, 'wb') as f:
for chunk in pbar:
f.write(chunk)
f.flush()
pbar.update(block_size)
pbar.refresh()
f.seek(0, 0)
if file_type == '':
z = None
elif file_type == 'zip':
z = zipfile.ZipFile(f)
elif file_type == 'tar':
z = tarfile.open(fileobj=f)
else:
raise ValueError("file type not supported")
if z and extract_dir:
z.extractall(extract_dir)
z.close()