Path.py (jaraco/path)
The 'path' library provides an object-oriented approach to file system paths, building upon the functionality of `os.path`. Distinct from the built-in `pathlib.Path`, `path.Path` objects are implemented as subclasses of `str`, allowing them to often be passed directly to functions expecting string paths without explicit conversion. It offers a range of convenience methods for common file and directory operations, aiming for a more Pythonic and robust way to interact with the filesystem. The current version is 17.1.1, and the project maintains a fairly active release schedule with several updates per year.
Warnings
- breaking Version 12.0 significantly changed compatibility, dropping support for Python 2.7 and Python 3.4. Code relying on these older Python versions will break.
- deprecated The `.text` method was deprecated in version 13.1.0 in favor of `.read_text()` (for text) and `.read_bytes()` (for binary data). Using `.text` might lead to incorrect newline handling or encoding issues, and it requires an `encoding` parameter if the default is insufficient.
- breaking Version 7.0 introduced changes to `open`, `write_text`, and `text` methods. The `open` method now uses `io.open` and will consistently raise an `OSError` on failure. The `text` method always returns text (never bytes) and requires an `encoding` parameter if the default is not suitable for decoding file content.
- gotcha `path.Path` objects are subclasses of `str`, allowing direct use where strings are expected. However, for APIs expecting `pathlib.Path` objects, there might be subtle behavioral differences, although `path` aims for `pathlib` compatibility where possible. Be cautious when mixing with code heavily relying on `pathlib`'s specific object behavior or type checks.
- gotcha When creating directories, `Path.mkdir()` does not automatically create parent directories nor does it have an `exist_ok` parameter like `pathlib.Path.mkdir()`. It will raise an error if parent directories do not exist or if the directory already exists.
Install
-
pip install path
Imports
- Path
from path import Path
Quickstart
from path import Path
import os
# Get current working directory
current_dir = Path.cwd()
print(f"Current directory: {current_dir}")
# Create a new path by joining components
# The '/' operator is overloaded for path concatenation
my_dir = current_dir / "temp_data"
my_file = my_dir / "example.txt"
print(f"Target file path: {my_file}")
# Ensure parent directories exist before creating the file
my_dir.makedirs_p() # Creates parent directories if they don't exist
# Write content to a file
my_file.write_text("Hello, path.py!\nThis is a test file.", encoding='utf-8')
# Read content from a file
content = my_file.read_text(encoding='utf-8')
print(f"Content of {my_file.name}:\n{content}")
# Check if path exists
if my_file.exists():
print(f"'{my_file.name}' exists.")
# Iterate over files with a pattern (globbing)
print("Python files in current directory:")
for f in current_dir.files("*.py"):
print(f"- {f.name}")
# Clean up: remove the created file and its parent directory
my_file.remove()
my_dir.rmdir_p() # Removes the directory and its parents if empty