Skip to main content

My short description for my project.

Project description

StrPathlib

Python Version License

StrPathlib is a lightweight enhancement to Python's standard pathlib library. By providing a mixin class StrPath, it enables all path objects (Path, PosixPath, WindowsPath, etc.) to directly invoke string instance methods (such as .upper(), .startswith(), .replace()), eliminating the need to manually convert to a string each time.

Features

  • Seamless Integration: StrPathlib.Path is fully compatible with pathlib.Path; all original path methods are preserved.
  • 🧵 Strings as Paths: Path objects can directly use 40+ string methods, such as .split(), .find(), .strip().
  • 🧩 Pure Python Implementation: No compilation required – just copy and use.
  • 📦 Lightweight: Zero dependencies outside the standard library.
  • 🔄 Complete Re-export: Re-exports all classes from pathlib (Path, PosixPath, PurePath, etc.), replaced with enhanced versions.

Installation

You can simply copy strPathlib.py into your project, or install via PyPI:

pip install strpathlib

Quick Start

from strPathlib import Path

p = Path("hello/world.txt")

# Original pathlib functionality works as usual
print(p.parent)          # Path('hello')
print(p.suffix)          # '.txt'
print(p.exists())        # True / False

# New: Directly call string methods
print(p.upper())         # 'HELLO/WORLD.TXT'
print(p.find("world"))   # 6
print(p.startswith("hello"))  # True
print(p.replace(".txt", ".md"))  # 'hello/world.md'

You can also import other enhanced classes:

from strPathlib import PosixPath, WindowsPath, PurePath

posix = PosixPath("/usr/bin/python3")
win = WindowsPath("C:/Users/Admin")
pure = PurePath("a/b/c")

Core Principles

StrPath is a mixin class that implements all public instance methods of str (approximately 45). Each method internally retrieves the string representation of the path via self.as_posix() and delegates to the actual string method.

Since pathlib.Path and its subclasses already provide the as_posix() method (which returns a path string using forward slashes), StrPath can safely be used with them.

Through multiple inheritance, the full behavior of pathlib.Path is preserved while gaining the set of string methods.

Note: The return values of string methods are ordinary Python strings (str), booleans, or lists, not new Path objects. If you need to continue using path functionality, wrap the result again: Path(p.upper()).

Relationship with Standard pathlib

This module does not modify the standard library's pathlib. It simply creates new classes via subclassing and binds them to the same top-level names as in pathlib. Therefore, it can safely be used as a drop-in replacement:

# Original code
from pathlib import Path

# Change to
from strPathlib import Path

If you need both the standard library's original classes and the enhanced ones, you can import like this:

import pathlib
from strPathlib import Path as StrPath

original = pathlib.Path("a.txt")
enhanced = StrPath("a.txt")

API Reference

StrPath

Mixin base class; should not be instantiated alone.

Requirement: The target class mixed in must provide an as_posix() method (returning a str).

Enhanced Path Classes

  • Path(pathlib.Path, StrPath)
  • PosixPath(pathlib.PosixPath, StrPath)
  • PurePath(pathlib.PurePath, StrPath)
  • PurePosixPath(pathlib.PurePosixPath, StrPath)
  • PureWindowsPath(pathlib.PureWindowsPath, StrPath)
  • WindowsPath(pathlib.WindowsPath, StrPath)

The constructors, attributes, and methods of these classes are identical to the corresponding pathlib classes.

Notes

  1. Platform Specifics: String methods are based on the result of as_posix() (always using forward slashes /). On Windows, as_posix() converts backslashes to forward slashes. This usually does not affect string operations, but be aware if you rely on platform-specific separators.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strpathlib-0.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strpathlib-0.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file strpathlib-0.1.0.tar.gz.

File metadata

  • Download URL: strpathlib-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for strpathlib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7b493b91180509b3ea49d532a03130274037fd162667d9aedd569a7973189ae7
MD5 4ccf5d69c07ae5d5cda0eed84b8f6aaa
BLAKE2b-256 1b5faa679568b736917d2819858c5906033019e4110dd64b7c34276eb9ee8479

See more details on using hashes here.

File details

Details for the file strpathlib-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: strpathlib-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for strpathlib-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 16daf9bc20ec79ddb8e9c0554102c6bbb804baa0a12f703afd7a1613a4571527
MD5 a1b0469d029e197c7337891d8f74dedb
BLAKE2b-256 f2cfddabe93cdb8f314c61ad4b5c8349871988a604e4a2c7a927d3f2e85ea1de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page