Skip to main content

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.

Metadata

Release files for strPathlib 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strPathlib 0.1.1
File Size Uploaded
strpathlib-0.1.1.tar.gz 9.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for strPathlib 0.1.1
File Interpreter ABI Platform
strpathlib-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.8 kB

Release files / strpathlib-0.1.1.tar.gz

Download URL strpathlib-0.1.1.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bf86532953cdd05af77e0eaab95fed0845bde8bbeb9bb4b3bc650ce310e99b84
BLAKE2b-256 checksum
How to use checksums
55b358fcd79f3d989d913f0b3833a80fd99851d4d716451d66d1543ee02ab948
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.10

Release files / strpathlib-0.1.1-py3-none-any.whl

Download URL strpathlib-0.1.1-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
68834c2ab897b8a28cac23e98e4073dfe8439ef97647a809ed68ba634fcb3540
BLAKE2b-256 checksum
How to use checksums
f50d1862c3876fa955c95fbc6bed59911c507837a7574241a198ab3b4a311ffe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page