File Path Operations (pathlib)
File Path Operations (pathlib) is a reusable Python snippet. Modern object-oriented file path handling with pathlib, which is more readable than os.path. The full implementation: from pathlib import Path # Create paths home = Path.home() cwd = Path.cwd() data_dir = cwd / "data" # Joins with / # Check properties data_dir.exists() # True/False data_dir.is_dir() # True/False data_dir.is_file() # True/False # Read/write files content = data_dir.read_text() # Read entire file data_dir.write_text("Hello!") # Write text data_dir.read_bytes() # Read binary # List files for f in Path(".").glob("*.py"): # All .py files print(f.name, f.stem, f.suffix) # Recursive listing for f in Path(".").rglob("*.py"): # All .py recursively print(f.relative_to(Path.cwd())) # Create/remove… It belongs to the Pathlib Module area of the standard library. This snippet uses only the Python standard library with no third-party dependencies, so it runs on any modern Python 3 interpreter. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.