Registry / pathlib2

pathlib2

JSON →
library2.3.7.post1pypypi✓ verified 23d ago

pathlib2 is a backport of the standard library module `pathlib` to Python versions 2.6, 2.7, 3.2, and 3.3. It provides an object-oriented filesystem path abstraction. For Python 3.4 and up, `pathlib` is part of the standard library, making `pathlib2` generally unnecessary for modern projects. The current version is 2.3.7.post1, and it's in community maintenance.

pip install pathlib2
INSTALL
IMPORT
SIG · PATHLIB2
P
pathlib2
pythonv2.3.7.post1
Install
1.6s avg
Import
40ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.3.7.post1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.034s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.046s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Path
from pathlib2 import Path
from pathlib import Path
When using `pathlib2`, ensure you import `Path` from `pathlib2`, not the standard library's `pathlib`.

This quickstart demonstrates basic usage of `pathlib2.Path` to represent filesystem paths, check existence, manipulate path components, and create/delete files and directories.

from pathlib2 import Path # Create a Path object p = Path('/tmp/my_file.txt') # Check if it exists print(f"Does {p} exist? {p.exists()}") # Get parent directory print(f"Parent directory: {p.parent}") # Get file name print(f"File name: {p.name}") # Create a new path by joining new_dir = Path('/tmp/my_data') new_dir.mkdir(exist_ok=True) new_file = new_dir / 'output.log' new_file.touch() print(f"New file created: {new_file.exists()}") print(f"Contents of new_file: {new_file.read_text()}") # Clean up new_file.unlink() new_dir.rmdir()
Debug
Known issues
breakingFor Python 3.4 and newer, use the standard library module `pathlib` instead of `pathlib2`. `pathlib2` is a backport for older Python versions only.
fix
Remove `pathlib2` from your dependencies and change imports from `from pathlib2 import Path` to `from pathlib import Path`.
affects: All Python versions >= 3.4
gotcha`pathlib2`'s API reflects an older state of the standard library `pathlib`. It does not include features, methods, or bug fixes introduced in `pathlib` in more recent Python versions (e.g., Python 3.5+).
fix
If you require newer `pathlib` features, you must migrate to a Python version where the standard library `pathlib` includes those features and switch away from `pathlib2`.
affects: All `pathlib2` versions compared to `pathlib` in Python 3.5+
gotchaThe `pathlib2` project is maintained by Jazzband, which implies community maintenance for its intended purpose (older Python versions). Active feature development or significant updates to align with the latest `pathlib` API are not expected.
fix
Be aware that `pathlib2` is stable but not actively evolving. For projects needing active development or modern `pathlib` features, prioritize Python versions with a current standard library `pathlib`.
affects: All `pathlib2` versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pathlib2'
The `pathlib2` package is not installed in the current Python environment or the environment where the code is being executed.
fix
Install the package using pip: `pip install pathlib2` or `pip3 install pathlib2` if you are targeting a specific Python 3 environment.
TypeError: argument should be a str object or an os.PathLike object returning str, not <class 'pathlib.PosixPath'>
This error often occurs in Python environments (typically Python < 3.4 or 3.5) where `pathlib2` is explicitly used, but a `pathlib.Path` object from the standard library `pathlib` (which might be incidentally installed or available) is passed to a function expecting a `pathlib2.Path` or a string.
fix
Ensure consistent usage of either `pathlib` or `pathlib2`. If targeting older Python versions, always import from `pathlib2` and convert any `pathlib.Path` objects to `pathlib2.Path` or a string if necessary, e.g., `import pathlib2 as pathlib` and explicitly convert objects with `pathlib2.Path(str(some_pathlib_path))`.
TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
This error arises when attempting to concatenate a `pathlib2.Path` object (or `pathlib.Path` from the standard library) directly with a string using the `+` operator, as `Path` objects do not support string concatenation.
fix
Use the `/` operator for path concatenation, which is the idiomatic way to join paths with `pathlib` and `pathlib2`. Alternatively, convert the `Path` object to a string explicitly using `str()` before concatenation if string operations are truly needed. Example: `my_path / 'filename.txt'` or `str(my_path) + '.bak'`.
AttributeError: 'PosixPath' object has no attribute 'rfind'
This error occurs when a method specific to string objects, like `rfind` (often used with `os.path`), is called on a `pathlib2.Path` object (or `pathlib.Path`). `Path` objects are not strings and have their own set of methods for path manipulation.
fix
Instead of string-based methods, use the object-oriented methods provided by the `pathlib2.Path` object. For example, to get the directory name, use `.parent`; to get the filename, use `.name`. If a string representation is absolutely required for string methods, convert the `Path` object to a string first using `str(my_path).rfind(...)`.
Upgrade
Version history
2.3.7.post1latest on PyPI · released Feb 10, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Resources
pathlib2 — pip install pathlib2 · libregistry