Python Download File from URL using Requests

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

Comments

Loading...