Engg-SS_ChatBOT / download_and_extract_zip.py
abhivsh's picture
Update download_and_extract_zip.py
a97612b verified
import requests
import zipfile
import os
URL = os.getenv('URL')
def download_and_extract_zip(url, destination_folder):
"""Downloads a zip file from a URL and extracts its contents to the specified destination folder."""
zip_file_path = "temp.zip"
try:
# Send an HTTP GET request to the OneDrive link to download the file
response = requests.get(url)
# Check if the request was successful (status code 200)
response.raise_for_status()
# Save the zip file to a temporary location
with open(zip_file_path, "wb") as f:
f.write(response.content)
# Create the destination folder if it doesn't exist
os.makedirs(destination_folder, exist_ok=True)
# Extract the contents of the zip file
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
zip_ref.extractall(destination_folder)
print(f"Zip file downloaded and extracted to: {destination_folder}")
except requests.exceptions.RequestException as e:
print(f"Error downloading file: {e}")
finally:
# Remove the temporary zip file
if os.path.exists(zip_file_path):
os.remove(zip_file_path)
# Call the function to download and extract the data
if __name__ == "__main__":
destination_folder = os.getcwd()
download_and_extract_zip(URL, destination_folder)