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 pathlib2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic usage of `pathlib2.Path` to represent filesystem paths, check existence, manipulate path components, and create/delete files and directories.
Remove `pathlib2` from your dependencies and change imports from `from pathlib2 import Path` to `from pathlib import Path`.
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`.
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`.
Install the package using pip: `pip install pathlib2` or `pip3 install pathlib2` if you are targeting a specific Python 3 environment.
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))`.
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'`.
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(...)`.
No dependency data recorded yet.