Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import os
- import zipfile
- def download_file(url, filename=None):
- # Define the Downloads folder path
- downloads_folder = os.path.join(os.path.expanduser("~"), "Downloads")
- # Use the provided filename or extract it from the URL
- if filename is None:
- filename = url.split("/")[-1]
- # Full path where the file will be saved
- file_path = os.path.join(downloads_folder, filename)
- # Download the file
- response = requests.get(url, stream=True)
- if response.status_code == 200:
- with open(file_path, "wb") as file:
- for chunk in response.iter_content(1024):
- file.write(chunk)
- #print(f"File downloaded successfully and saved as: {file_path}")
- else:
- print(f"Failed to download file. Status code: {response.status_code}")
- return None # Stop if the download failed
- # If the file is a zip, extract it
- if zipfile.is_zipfile(file_path):
- extract_folder = os.path.join(downloads_folder, filename.replace(".zip", ""))
- os.makedirs(extract_folder, exist_ok=True)
- with zipfile.ZipFile(file_path, "r") as zip_ref:
- zip_ref.extractall(extract_folder)
- # print(f"File extracted successfully to: {extract_folder}")
- # Optionally, remove the downloaded zip file after extraction
- os.remove(file_path)
- else:
- print("The downloaded file is not a zip file.")
- baseUrl = r'https://raw.githubusercontent.com/anubisthetyrant/dotnet/main/'
- download_file(baseUrl + 'programs.zip')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement