Download and Unzip a Zipped File in Python
In this example, you will learn the process of downloading and extracting zipped files. Once the download is complete, the code will also retrieve all file names based on their extensions from the directory where they were extracted:
import urllib.request
import os
import fnmatch
import zipfile
def download_and_unzip(download_url_link, dir_path, zipped_filename,destination_dir_name):
print("Download starting")
urllib.request.urlretrieve(
download_url_link, os.path.join(dir_path, zipped_filename)
)
print("Download complete")
print("unzipping file starting")
with zipfile.ZipFile(os.path.join(dir_path, zipped_filename), "r") as zip_file:
zip_file.extractall(os.path.join(dir_path, destination_dir_name))
print("unzipping complete")
def get_unzipped_filenames(path, pattern):
print("Getting unzipped files")
results = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
results.append(name)
print("File unzipping complete : ")
return results
if __name__ == "__main__":
# Replace this link with the download link
download_url_link = "https://www.python.org/ftp/python/3.10.1/python-3.10.1-embed-amd64.zip"
#download zipped file name
zipped_filename = "python.zip"
#directory name where the downloaded file will be extracted
last_index = zipped_filename.rfind(".")
destination_dir_name = zipped_filename[0:last_index]
dir_path = os.path.dirname(os.path.realpath(__file__))
extracted_to_dir = os.path.join(dir_path, destination_dir_name)
#pattern to find all the .dll files
filename_pattern = "*.dll"
download_and_unzip(download_url_link, dir_path,zipped_filename, extracted_to_dir)
extracted_filenames = get_unzipped_filenames(extracted_to_dir, filename_pattern)
print("Unzipped files : ", extracted_filenames)