Pathy: Cloud-Agnostic Pathlib

0.13.0 · active · verified Thu Apr 09

Pathy extends Python's `pathlib.Path` to provide a unified, pathlib-compatible interface for interacting with local filesystems and various cloud storage services (S3, GCS, Azure Blob Storage). It aims to make file operations cloud-agnostic and seamless. The current stable version is 0.13.0, with frequent minor and patch updates.

Warnings

Install

Imports

Quickstart

This example demonstrates basic file operations using Pathy for both local and cloud (S3) paths. For cloud operations, ensure the relevant `pathy` extras are installed (e.g., `pathy[s3]`) and environment variables for authentication are configured.

from pathy import Pathy
import os

# --- Local filesystem operations ---
local_file_path = Pathy("./temp_pathy_local_file.txt")
try:
    local_file_path.write_text("Hello from Pathy on local filesystem!")
    print(f"Local file content: '{local_file_path.read_text()}'")
    print(f"Local file size: {local_file_path.stat().st_size} bytes")
finally:
    if local_file_path.exists():
        local_file_path.unlink() # Clean up

# --- Cloud storage operations (example: S3) ---
# Replace with your bucket name and ensure AWS credentials are set as environment variables
s3_bucket = os.environ.get("PATHY_TEST_S3_BUCKET", "my-pathy-test-bucket")
s3_path = Pathy(f"s3://{s3_bucket}/example_dir/cloud_data.txt")

# Check if AWS credentials are likely available
if os.environ.get("AWS_ACCESS_KEY_ID") and os.environ.get("AWS_SECRET_ACCESS_KEY"):
    print(f"\nAttempting S3 operations on: {s3_path}")
    try:
        # Create parent directories (no-op if they exist for cloud paths)
        s3_path.parent.mkdir(parents=True, exist_ok=True)
        s3_path.write_text("Hello from Pathy on S3!")
        print(f"S3 file content: '{s3_path.read_text()}'")
        print(f"S3 file size: {s3_path.stat().st_size} bytes")
    except Exception as e:
        print(f"Failed to perform S3 operations. Ensure 'pathy[s3]' is installed and credentials are correct. Error: {e}")
    finally:
        if s3_path.exists():
            s3_path.unlink() # Clean up
            print(f"Cleaned up S3 file: {s3_path}")
else:
    print("\nSkipping S3 example: AWS_ACCESS_KEY_ID and/or AWS_SECRET_ACCESS_KEY environment variables not set.")
    print("Install 'pathy[s3]' and configure AWS credentials to run this example.")

view raw JSON →