@@ -26,7 +26,7 @@ def _create_if_not_exists(path: str, remove=True) -> None:
26
26
27
27
os .makedirs (path , exist_ok = True )
28
28
29
- def download (url : str , filename : str , unzip = False , unzip_path : str = None , force_download = False , force_unzip = False , clean = False ) -> None :
29
+ def download (url : str , filename : str , unzip = False , unzip_path : str = None , force_download = False , force_unzip = False , clean = False ) -> str :
30
30
"""
31
31
Download a file from a OneDrive url.
32
32
@@ -38,10 +38,14 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
38
38
:param force_download: Whether to force download the file even if it already exists.
39
39
:param force_unzip: Whether to force unzip and overwrite the file even if it already exists.
40
40
:param clean: Whether to clean the unzipped files after unzipping.
41
+
42
+ :returns: Path of the downloaded (and extracted if 'unzip') file.
41
43
"""
42
44
assert url is not None , "URL cannot be None!"
43
45
assert filename is not None , "Parameter filename cannot be None!"
44
46
47
+ ret_path = None
48
+
45
49
if not url .endswith ("?download=1" ):
46
50
# replace everithing after the last ? with ?download=1
47
51
url = url .split ("?" )[0 ] + "?download=1"
@@ -60,6 +64,8 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
60
64
progress_bar = tqdm (total = total_size_in_bytes , unit = 'iB' , unit_scale = True ) if total_size_in_bytes > 1024 else None
61
65
block_size = 1024
62
66
67
+ ret_path = filename
68
+
63
69
if not os .path .exists (filename ) or force_download :
64
70
_create_if_not_exists (filename )
65
71
@@ -81,7 +87,8 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
81
87
if filename .endswith (".zip" ):
82
88
unzip_path = unzip_path if unzip_path is not None else os .path .split (filename )[0 ]
83
89
clean_unzip_path = force_unzip and os .path .realpath (unzip_path ) not in os .path .realpath (filename )
84
-
90
+ ret_path = unzip_path
91
+
85
92
_create_if_not_exists (unzip_path , remove = clean_unzip_path )
86
93
87
94
if force_unzip :
@@ -95,6 +102,7 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
95
102
if clean :
96
103
os .remove (filename )
97
104
105
+ return ret_path
98
106
except Exception as e :
99
107
print (e )
100
108
raise Exception ("ERROR, something went wrong, see error above!" )
@@ -103,4 +111,5 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
103
111
if __name__ == "__main__" :
104
112
ln = "https://unimore365-my.sharepoint.com/:u:/g/personal/215580_unimore_it/EQ-DxzGOF7lBt90A601kvVEBR_ca9PtUdN_asesZ-F80bw?download=1"
105
113
print ('Downloading dataset' )
106
- download (ln , filename = "./tmp/" , unzip = True )
114
+ ret = download (ln , filename = "./tmp/" , unzip = True )
115
+ print (ret )
0 commit comments