Python Download File from URL using Requests
Author:CodeSnippets
Language:
Python
6 views
0 favorites
3 hours ago
Code Implementation
Python
import requests
def download_file(url, filename):
response = requests.get(url, stream=True)
with open(filename, "wb") as f:
for chunk in response.iter_content(1024):
f.write(chunk)
# Usage
download_file("https://example.com/file.zip", "output.zip")
Use the requests library to download files from a URL and save them locally.
#requests#download
Snippet Description
- Use
stream=True
to prevent loading large files all at once - It is recommended to add exception handling and check if
status_code == 200
- Can be used to download datasets, images, and configuration files
Recommended Snippets
Python Remove Duplicates from List of Dicts
Remove duplicates from a list of dictionaries based on a specific key in the dictionaries.
Python
#dict+1
6
0
Python Read Nested JSON File Example
How to read JSON files with nested structures in Python and safely access deep-level fields.
Python
#json+1
8
0
Python: Write Dict to CSV with Headers
Write a list of dictionaries to a CSV file and automatically generate headers.
Python
#dict+1
6
0
Comments
Loading...