Skip to main content

How to Download a File from a URL inPython

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.

# Install the requests and wget libraries
pip install requests wget
note

If you are using Python 3, you may need to use the pip3 command instead: pip3 install requests wget.

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 of the file to be downloaded
url = "https://github.com/favicon.ico"

try:
# Send a GET request to the URL
response = requests.get(url)

# Check if the request was successful (status code 200)
response.raise_for_status()

# Open a local file in write-binary mode and write the content
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.
note

We use the 'wb' mode (w for write, b for binary) when opening the file. This is crucial for non-text files like images, zip archives, or executables, as it prevents Python from trying to interpret the bytes as text.

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:
# Download the file and save it with a specific name
# If the second argument is omitted, the original filename is used.
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.ico

Method 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:
# Download the file from the URL and save it locally
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 of a large file (e.g., a sample video)
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

LibraryBest ForKey Features
requestsMost use cases.Simple API, robust error handling, supports streaming for large files. Highly recommended.
wgetSimple, direct downloads.Very easy to use, provides a progress bar automatically.
urllibEnvironments 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.