Downloading files from the web is a common task in Python, essential for everything from web scraping to data acquisition. Python's rich ecosystem offers several excellent libraries for this purpose. The most popular third-party library is requests, known for its simplicity and power, while the standard library provides the built-in urllib module for cases where external dependencies are not an option.
This guide will demonstrate three practical methods to download a file from a URL using the requests, wget, and urllib libraries, with a special focus on the recommended requests approach.
Prerequisites: Installing Libraries
The requests and wget libraries are third-party packages and must be installed first using pip. The urllib module is part of Python's standard library, so no installation is needed for it.
pip install requests wgetMethod 1: Using the requests Library (Recommended)
The requests library is the de facto standard for making HTTP requests in Python. Its API is simple, elegant, and powerful, making it the best choice for most applications.
The process involves making a GET request to the URL and then writing the binary content of the response to a local file.
Solution:
import requests
url = "https://github.com/favicon.ico"
try:
response = requests.get(url)
response.raise_for_status()
with open("github.ico", "wb") as f:
f.write(response.content)
print("File downloaded successfully.")
except requests.exceptions.RequestException as e:
print(f"Error downloading file: {e}")Output:
File downloaded successfully.Method 2: Using the wget Library
The wget library is a simple, lightweight wrapper that mimics the behavior of the popular wget command-line tool. It provides a very direct way to download a file.
Solution:
import wget
url = "https://github.com/favicon.ico"
try:
filename = wget.download(url, out="github-wget.ico")
print(f"\nFile downloaded successfully: {filename}")
except Exception as e:
print(f"\nError downloading file: {e}")Output (wget provides a progress bar):
100% [................................................] 32031 / 32031
File downloaded successfully: github-wget.icoMethod 3: Using the Built-in urllib Library
If you cannot install external libraries, Python's built-in urllib.request module can get the job done. The urlretrieve function is a high-level helper that handles the entire process.
Solution:
import urllib.request
url = "https://github.com/favicon.ico"
filename = "github-urllib.ico"
try:
urllib.request.urlretrieve(url, filename)
print("File downloaded successfully.")
except Exception as e:
print(f"Error downloading file: {e}")Output:
File downloaded successfully.Bonus: Downloading Large Files with Streaming (requests)
When downloading large files, loading the entire response.content into memory at once (as in Method 1) can be very inefficient. The requests library provides a "streaming" mechanism to handle this. By setting stream=True, the response content is downloaded in chunks.
Solution for Large Files:
import requests
url = "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4"
filename = "large_video_file.mp4"
chunk_size = 8192 # 8 KB
try:
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=chunk_size):
f.write(chunk)
print(f"Large file '{filename}' downloaded successfully.")
except requests.exceptions.RequestException as e:
print(f"Error downloading file: {e}")Output:
Large file 'large_video_file.mp4' downloaded successfully.Conclusion
| Library | Best For | Key Features |
|---|---|---|
requests |
Most use cases. | Simple API, robust error handling, supports streaming for large files. Highly recommended. |
wget |
Simple, direct downloads. | Very easy to use, provides a progress bar automatically. |
urllib |
Environments with no external libraries allowed. | Built-in to Python, no installation needed. |
For most projects, the requests library offers the best balance of simplicity, power, and flexibility. For simple, one-off downloads, wget is a great choice, and urllib remains a reliable fallback when you are restricted to the standard library.