Universal Pathlib

0.3.10 · active · verified Sat Mar 28

Universal Pathlib is a Python library that extends the `pathlib.Path` interface to provide a unified, Pythonic way of interacting with a wide variety of backend filesystems, including local, cloud storage (S3, GCS, Azure), HTTP(S), and more, by leveraging `fsspec` backends. It is currently at version 0.3.10 and maintains a regular release cadence with frequent updates and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `UPath` for both a remote S3 bucket and a local file. It shows common operations like writing text, reading text, checking existence, and iterating directory contents. For S3, ensure `s3fs` is installed and AWS credentials are configured (e.g., via environment variables or `~/.aws/credentials`). For actual usage, replace `your-test-s3-bucket` with an accessible S3 bucket.

import os
from upath import UPath

# Example for S3 (requires 's3fs' to be installed and AWS credentials configured)
# You can use environment variables for credentials or pass storage_options
S3_BUCKET = os.environ.get('S3_TEST_BUCKET', 'your-test-s3-bucket')
S3_KEY = 'example.txt'

# Create a UPath object for an S3 location
s3path = UPath(f"s3://{S3_BUCKET}") / S3_KEY

# Write content to the S3 file
try:
    s3path.write_text('Hello from Universal Pathlib!')
    print(f"Successfully wrote to {s3path}.")

    # Read content from the S3 file
    content = s3path.read_text()
    print(f"Content of {s3path}: '{content}'.")

    # Check if the path exists
    print(f"Does {s3path} exist? {s3path.exists()}.")

    # List contents of the parent directory (if bucket exists and is not empty)
    print(f"Listing contents of {s3path.parent}:")
    for p in s3path.parent.iterdir():
        print(f"  - {p}")

finally:
    # Clean up: delete the created file
    if s3path.exists():
        s3path.unlink()
        print(f"Cleaned up: deleted {s3path}.")

# Example for a local file (no additional fsspec backend needed)
local_path = UPath("local_example.txt")
local_path.write_text("This is a local file.")
print(f"Local file content: {local_path.read_text()}")
local_path.unlink()

view raw JSON →