2121#
2222
2323import hashlib
24- import os
2524import re
2625import warnings
26+ from pathlib import Path
2727from typing import Optional
2828
2929import requests
@@ -57,8 +57,10 @@ def download_file_with_progress_bar(url: str,
5757 """
5858
5959 # Check if the file already exists in the location
60- file_path = os .path .join (location , file_name )
61- if os .path .exists (file_path ):
60+ location_path = Path (location )
61+ file_path = location_path / file_name
62+
63+ if file_path .exists ():
6264 existing_checksum = calculate_checksum (file_path )
6365 if existing_checksum == expected_checksum :
6466 return file_path
@@ -91,10 +93,10 @@ def download_file_with_progress_bar(url: str,
9193 )
9294
9395 # Create the directory and any necessary parent directories
94- os . makedirs ( location , exist_ok = True )
96+ location_path . mkdir ( exist_ok = True )
9597
9698 filename = filename_match .group (1 )
97- file_path = os . path . join ( location , filename )
99+ file_path = location_path / filename
98100
99101 total_size = int (response .headers .get ("Content-Length" , 0 ))
100102 checksum = hashlib .md5 () # create checksum
@@ -111,7 +113,7 @@ def download_file_with_progress_bar(url: str,
111113 downloaded_checksum = checksum .hexdigest () # Get the checksum value
112114 if downloaded_checksum != expected_checksum :
113115 warnings .warn (f"Checksum verification failed. Deleting '{ file_path } '." )
114- os . remove ( file_path )
116+ file_path . unlink ( )
115117 warnings .warn ("File deleted. Retrying download..." )
116118
117119 # Retry download using a for loop
0 commit comments