Requests-File

3.0.1 · active · verified Sat Mar 28

Requests-File is a transport adapter for the popular Python Requests library, enabling local filesystem access via `file://` URLs. It allows `requests.Session` objects to fetch local files as if they were remote resources. The current version is 3.0.1, and it follows the release cadence of its underlying dependency, `requests`, which is actively maintained and frequently updated.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `requests.Session`, mount the `FileAdapter`, and then make a GET request to a local file using a `file://` URL. It includes error handling and cleanup for the example file.

import requests
from requests_file import FileAdapter
import os

# Create a dummy file for demonstration
file_content = "Hello from local file!"
file_path = "./test_file.txt"
with open(file_path, "w") as f:
    f.write(file_content)

s = requests.Session()
s.mount('file://', FileAdapter())

# Construct a file:// URL (absolute path is recommended)
absolute_file_path = os.path.abspath(file_path)
file_url = f'file://{absolute_file_path}'

try:
    response = s.get(file_url)
    response.raise_for_status() # Raise an exception for HTTP errors
    print(f"Status Code: {response.status_code}")
    print(f"Content: {response.text}")
except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the dummy file
    os.remove(file_path)

view raw JSON →