cached-path

1.8.10 · active · verified Mon Apr 13

cached-path is a Python file utility library that provides a unified, simple interface for accessing both local and remote files. It automatically downloads and caches remote resources, making them available as local file paths. Currently at version 1.8.10, the library maintains an active development pace with frequent patch and minor releases to address compatibility and add new features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `cached_path()` to download and cache a remote file, and how to extract an archive. It verifies that the returned paths exist locally.

import os
from cached_path import cached_path

# Download and cache a remote file
remote_url = "https://raw.githubusercontent.com/allenai/cached_path/main/README.md"
local_path = cached_path(remote_url)
print(f"Cached file path: {local_path}")
assert os.path.exists(local_path)

# Example with an archive, extracting it
archive_url = "https://github.com/allenai/cached_path/releases/download/v0.1.0/cached_path-0.1.0.tar.gz"
extracted_dir = cached_path(archive_url, extract_archive=True)
print(f"Extracted archive directory: {extracted_dir}")
assert os.path.isdir(extracted_dir)

# Clean up (optional, for demonstration)
# import shutil
# shutil.rmtree(os.path.dirname(local_path))
# shutil.rmtree(extracted_dir)

view raw JSON →