I have spent over a decade writing Python scripts to automate data workflows. One task that pops up constantly is grabbing compressed data from the web.
Whether I am pulling census records or financial reports, knowing how to handle ZIP files efficiently saves me hours of manual clicking.
In this tutorial, I will show you exactly how I use Python to download and extract ZIP files directly from a URL.
Download and Extract ZIP Files from a URL Using Python
Downloading and extracting ZIP files from a URL is a common requirement in data science, web scraping, and other areas of software development.
Python provides several libraries that make this task straightforward. Now, let me show you different methods for downloading and extracting ZIP files using Python with examples.
Method 1: Use requests and zipfile in Python
You can use the requests library to download files and the zip file library to extract a zip file. This is the most recommended approach, and as a data science developer, I always use this method.
- Install the Required Libraries First, ensure you have the
requestslibrary installed. If not, you can install it using pip:
pip install requests- Download a ZIP File. Here’s a complete example:
Here is the complete code to download a zip file from a URL in Python.
# importing the requests module
import requests
print('Downloading started')
url = 'https://pythonguides.com/....../MyPDFFile.zip'
# Downloading the file by sending the request to the URL
req = requests.get(url)
# Split URL to get the file name
filename = url.split('/')[-1]
# Writing the file to the local file system
with open(filename,'wb') as output_file:
output_file.write(req.content)
print('Downloading Completed')I executed the above example code and added the screenshot below.

- Download and Extract the ZIP File. Here’s a complete example:
import requests
from zipfile import ZipFile
from io import BytesIO
# URL of the ZIP file
url = 'https://pythonguides.com/....../MyPDFFile.zip'
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Create a ZipFile object from the response content
with ZipFile(BytesIO(response.content)) as zip_file:
# Extract all contents to the current directory
zip_file.extractall()
print("Files extracted successfully")
else:
print(f"Failed to download file: {response.status_code}")- Sending the Request: We use requests.get(url) to send a GET request to the specified URL.
- Creating a ZipFile Object: We use BytesIO to handle the binary content and pass it to ZipFile.
- Extracting Files: The extractall() method extracts all files from the ZIP archive to the current directory.
Method 2: Use Python’s urllib and zipfile
Another way to download files from a URL in Python is by using the urllib library, which is included in Python’s standard library.
- Download and Extract the ZIP File
import urllib.request
from zipfile import ZipFile
import os
# URL of the ZIP file
url = 'https://pythonguides.com/....../MyPDFFile.zip'
file_name = 'downloaded_file.zip'
# Download the file from the URL
urllib.request.urlretrieve(url, file_name)
# Extract the downloaded ZIP file
with ZipFile(file_name, 'r') as zip_file:
zip_file.extractall()
print("Files extracted successfully")
# Optional: Remove the ZIP file after extraction
os.remove(file_name)- Downloading the File: urllib.request.urlretrieve(url, file_name) downloads the file and saves it locally.
- Extracting Files: Similar to the previous method, we use ZipFile to extract the contents.
- Cleanup: Optionally, you can remove the ZIP file after extraction using os.remove(file_name).
Method 3: Use wget and zipfile in Python
The wget library is another alternative for downloading files, especially useful for large files due to its robust handling of network issues.
- Install the wget Library
pip install wget- Download and Extract the ZIP File
import wget
from zipfile import ZipFile
import os
# URL of the ZIP file
url = 'https://pythonguides.com/....../MyPDFFile.zip'
file_name = wget.download(url)
# Extract the downloaded ZIP file
with ZipFile(file_name, 'r') as zip_file:
zip_file.extractall()
print("Files extracted successfully")
# Optional: Remove the ZIP file after extraction
os.remove(file_name)- Downloading the File: wget.download(url) downloads the file and returns the file name.
- Extracting Files: The ZipFile usage remains the same.
- Cleanup: Removing the ZIP file is optional, but it helps in keeping the workspace clean.
In this tutorial, I showed you several ways to use Python to download and extract ZIP files from a URL. We looked at the requests method for in-memory handling and the urllib method for larger files.
I hope you found this tutorial helpful!
You may like the following tutorial:
- Use the arange() Function in Python
- Use the Python Pass Function
- Use the trim() Function in Python
- Use the strip() Function in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.