A useful tool based on pathlib and make str methods.
Project description
StrPathlib
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.Pathis fully compatible withpathlib.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 newPathobjects. 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
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strpathlib-0.1.1.tar.gz.
File metadata
- Download URL: strpathlib-0.1.1.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf86532953cdd05af77e0eaab95fed0845bde8bbeb9bb4b3bc650ce310e99b84
|
|
| MD5 |
c3725d1324d8b6d1f8c605c825a1bfca
|
|
| BLAKE2b-256 |
55b358fcd79f3d989d913f0b3833a80fd99851d4d716451d66d1543ee02ab948
|
File details
Details for the file strpathlib-0.1.1-py3-none-any.whl.
File metadata
- Download URL: strpathlib-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68834c2ab897b8a28cac23e98e4073dfe8439ef97647a809ed68ba634fcb3540
|
|
| MD5 |
19e0b200fae6b957ec46907edc5dcc55
|
|
| BLAKE2b-256 |
f50d1862c3876fa955c95fbc6bed59911c507837a7574241a198ab3b4a311ffe
|