Pathlib Abstract Base Classes

0.5.2 · active · verified Sun Mar 29

pathlib-abc is a Python library that provides abstract base classes (ABCs) for `pathlib.Path`-like objects. It serves as a backport and preview of `pathlib` functionality intended for future Python standard library releases, specifically designed to enable path implementations for non-local filesystems (e.g., archives, remote storage). The library is currently at version 0.5.2 and is under active development, requiring Python 3.9+.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a custom path class by inheriting from `JoinablePath` and implementing the necessary abstract methods. It shows basic path manipulation using the overloaded division operator and accessing path components like `parent` and `name`.

from pathlib_abc import JoinablePath, PathInfo

class CustomPath(JoinablePath):
    _parser = JoinablePath._pathmod.posixpath # Example: use POSIX path parsing

    def __init__(self, path_str):
        self._path = path_str

    def __str__(self):
        return self._path

    @property
    def info(self) -> PathInfo:
        # In a real implementation, this would return actual file system info.
        # For this example, we return a basic PathInfo instance.
        return PathInfo(is_dir=True, is_file=False, is_symlink=False)

    def _with_segments(self, segments):
        return CustomPath(self._parser.join(*segments))

# Example usage:
base = CustomPath('/my/virtual/root')
file_path = base / 'folder' / 'document.txt'
print(f"Base path: {base}")
print(f"File path: {file_path}")
print(f"Parent of file_path: {file_path.parent}")
print(f"Name of file_path: {file_path.name}")

view raw JSON →