Requests-File

raw JSON →
3.0.1 verified Tue May 12 auth: no python install: verified quickstart: verified

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.

pip install requests-file
error ModuleNotFoundError: No module named 'requests_file'
cause The 'requests-file' library has not been installed or is not accessible in the current Python environment.
fix
Install the library using pip: pip install requests-file
error requests.exceptions.MissingSchema: Invalid URL 'my_local_file.txt': No schema supplied. Perhaps you meant http://my_local_file.txt?
cause The URL provided to requests lacks a scheme (like `http://`, `https://`, or `file://`), which is required for `requests-file` to handle local paths.
fix
Prefix the local file path with file://, e.g., requests.get('file:///path/to/my_local_file.txt').
error requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
cause The specified local file path in the `file://` URL does not exist, or the path is incorrectly formatted (e.g., using backslashes on Windows within the URL).
fix
Verify the file path is correct and the file exists. For Windows paths, ensure forward slashes are used, e.g., file:///C:/Users/User/document.txt.
gotcha When accessing `response.text`, the encoding is inferred by `chardet` (if installed) or defaults to `ISO-8859-1`. This might not always match the actual file encoding, potentially leading to incorrect decoding. For binary files or specific encodings, it's safer to use `response.content` and decode manually.
fix For explicit encoding, use `response.content.decode('your_encoding')`. For binary data, process `response.content` directly.
gotcha requests-file maps common file system errors to HTTP status codes: `EACCES` (permission denied) is converted to a 403 Forbidden, `ENOENT` (file not found) to a 404 Not Found, and other `IOError` types to a 400 Bad Request. Be aware of these mappings when implementing error handling.
fix Implement error handling expecting HTTP status codes 400, 403, and 404 for underlying filesystem issues.
gotcha As a `requests` transport adapter, `requests-file` relies on `requests`'s internal adapter API. Future major versions of the `requests` library might introduce breaking changes to this API (e.g., changes to `HTTPAdapter` or connection management), which could potentially affect `requests-file`'s functionality or require updates. For example, recent `requests` versions had changes related to `SSLContext` caching and the `_get_connection` method which could impact custom adapters.
fix Always test `requests-file` compatibility when upgrading the `requests` library to a new major version. Refer to `requests` release notes for `HTTPAdapter`-related changes.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.58s 21.2M
3.10 alpine (musl) - - 0.57s 21.2M
3.10 slim (glibc) wheel 2.3s 0.40s 22M
3.10 slim (glibc) - - 0.41s 22M
3.11 alpine (musl) wheel - 0.76s 23.2M
3.11 alpine (musl) - - 0.79s 23.2M
3.11 slim (glibc) wheel 2.3s 0.62s 24M
3.11 slim (glibc) - - 0.59s 24M
3.12 alpine (musl) wheel - 0.68s 15.0M
3.12 alpine (musl) - - 0.70s 15.0M
3.12 slim (glibc) wheel 2.0s 0.68s 16M
3.12 slim (glibc) - - 0.65s 15M
3.13 alpine (musl) wheel - 0.67s 14.8M
3.13 alpine (musl) - - 0.68s 14.7M
3.13 slim (glibc) wheel 1.9s 0.62s 15M
3.13 slim (glibc) - - 0.66s 15M
3.9 alpine (musl) wheel - 0.51s 20.4M
3.9 alpine (musl) - - 0.53s 20.5M
3.9 slim (glibc) wheel 2.6s 0.44s 21M
3.9 slim (glibc) - - 0.43s 21M

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)