Pathlib Mate

1.3.2 · active · verified Sat Apr 11

Pathlib Mate (pathlib-mate) is a Python library that extends and enhances the standard `pathlib` module, providing more powerful and user-friendly methods and attributes for path manipulation. It offers convenient attribute accessors for path components, advanced file and directory search capabilities, and utility methods for common file operations. The library is actively maintained, with releases occurring infrequently, the latest being in January 2024.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `Path` object from `pathlib-mate`, access its extended attributes like `abspath`, `basename`, `fname`, and `ext`, and perform powerful search operations using methods like `select_by_ext`. It creates and cleans up a temporary directory and files to illustrate functionality.

import os
from pathlib_mate import Path
import shutil

# Create a temporary directory and file for demonstration
test_dir = Path("pathlib_mate_demo_dir")
test_file = test_dir / "example.txt"

try:
    test_dir.mkdir(parents=True, exist_ok=True)
    test_file.write_text("Hello, pathlib-mate!")

    # 1. Basic Path Creation and Attribute Access
    p = Path(test_file.abspath)
    print(f"Absolute path: {p.abspath}")
    print(f"File name with extension: {p.basename}")
    print(f"File name without extension: {p.fname}")
    print(f"File extension: {p.ext}")
    print(f"Parent directory name: {p.dirname}")
    print(f"Parent directory path: {p.dirpath}")
    print(f"File size in bytes: {p.size}")
    print(f"File size (human readable): {p.size_in_text}")

    # 2. Powerful Path Search (example: find all text files)
    # Create another file for search demo
    (test_dir / "another.txt").write_text("Another file.")
    (test_dir / "image.jpg").write_bytes(b'fake_image_data')

    print("\nSearching for .txt files:")
    for txt_file in test_dir.select_by_ext(ext=".txt", recursive=True):
        print(f"  Found: {txt_file.basename}")

finally:
    # Clean up temporary directory
    if test_dir.exists():
        shutil.rmtree(test_dir.abspath)
    print("\nCleanup complete.")

view raw JSON →