On this page
pathlib
Prev: os | Up: stdlib_module | Next: pdb
Rel: os, io, time, fileinput, stat
Ref: docs.python.org — pathlib
Tags: #public #python3 #stdlib
Import
import pathlib
Overview
Object-oriented filesystem paths that replace string manipulation (os.path.join, os.path.dirname) with composable Path objects. Reach for pathlib.Path for virtually all path construction, traversal, globbing, and small file I/O; drop down to os only for low-level file descriptors, process environment variables, or OS-specific syscalls.
Quick Start
from pathlib import Path
base = Path(__file__).resolve().parent
config = base / "config" / "settings.json"
assert config.name == "settings.json"
assert config.stem == "settings"
assert config.suffix == ".json"
assert config.parent.name == "config"
Implementation Walkthrough
1. Safe Directory Creation, Atomic-Style Read/Write & Globbing
from pathlib import Path
workspace = Path("/tmp/demo_pathlib")
workspace.mkdir(parents=True, exist_ok=True)
notes = workspace / "notes.txt"
notes.write_text("hello world\n", encoding="utf-8")
assert notes.read_text(encoding="utf-8") == "hello world\n"
# Recursive search for all Markdown files under workspace
md_files = sorted(workspace.rglob("*.md"))
2. Pure Path Manipulation Without Touching Disk
Use PurePosixPath or PureWindowsPath when manipulating paths for another OS (such as URLs, container paths, or cross-platform manifests) without performing filesystem syscalls:
from pathlib import PurePosixPath
asset = PurePosixPath("/srv/app/static/logo.png")
assert asset.with_suffix(".webp") == PurePosixPath("/srv/app/static/logo.webp")
assert asset.relative_to("/srv/app") == PurePosixPath("static/logo.png")
Gotchas & Boundaries
- Leading
/resets the path:Path("/a/b") / "/c"evaluates toPath("/c"), notPath("/a/b/c"). Strip leading slashes when joining untrusted subpaths. - Always pass
encoding="utf-8":read_text()andwrite_text()default to the platform locale encoding (oftencp1252on Windows). unlink()fails on missing files unlessmissing_ok=True: Usepath.unlink(missing_ok=True)for idempotent cleanup, andshutil.rmtree(path)for non-empty directories.- Check
is_relative_to()before trusting user paths: Resolve both paths (candidate.resolve().is_relative_to(root.resolve())) to prevent path traversal.
Classes & Objects
- pathlib.Path — PurePath subclass that can make system calls. (Methods:
.absolute(),.anchor(),.as_posix(),.as_uri(),.chmod(),.copy()) - pathlib.PosixPath — Path subclass for non-Windows systems. (Methods:
.absolute(),.anchor(),.as_posix(),.as_uri(),.chmod(),.copy()) - pathlib.PurePath — Base class for manipulating paths without I/O. (Methods:
.anchor(),.as_posix(),.as_uri(),.drive(),.full_match(),.is_absolute()) - pathlib.PurePosixPath — PurePath subclass for non-Windows systems. (Methods:
.anchor(),.as_posix(),.as_uri(),.drive(),.full_match(),.is_absolute()) - pathlib.PureWindowsPath — PurePath subclass for Windows systems. (Methods:
.anchor(),.as_posix(),.as_uri(),.drive(),.full_match(),.is_absolute()) - pathlib.UnsupportedOperation — An exception that is raised when an unsupported operation is attempted. (Methods:
.add_note(),.with_traceback()) - pathlib.WindowsPath — Path subclass for Windows systems. (Methods:
.absolute(),.anchor(),.as_posix(),.as_uri(),.chmod(),.copy())
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/filecmp
- tuts/python3/stdlib_module/fileinput
- tuts/python3/stdlib_module/fnmatch
- tuts/python3/stdlib_module/genericpath
- tuts/python3/stdlib_module/glob
- tuts/python3/stdlib_module/io
- tuts/python3/stdlib_module/linecache
- tuts/python3/stdlib_module/nt
- tuts/python3/stdlib_module/ntpath
- tuts/python3/stdlib_module/os
- tuts/python3/stdlib_module/pathlib/Path
- tuts/python3/stdlib_module/pathlib/PosixPath
- tuts/python3/stdlib_module/pathlib/PurePath
- tuts/python3/stdlib_module/pathlib/PurePosixPath
- tuts/python3/stdlib_module/pathlib/PureWindowsPath
- tuts/python3/stdlib_module/pathlib/UnsupportedOperation
- tuts/python3/stdlib_module/pathlib/WindowsPath
- tuts/python3/stdlib_module/pdb
- tuts/python3/stdlib_module/posixpath
- tuts/python3/stdlib_module/pwd
- tuts/python3/stdlib_module/shutil
- tuts/python3/stdlib_module/stat
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tempfile
- tuts/python3/stdlib_module/time
- tuts/python3/tutorials/popular_application_domains/automation_devops_and_web_scraping
Related notes
- tuts/python3/stdlib_module/fileinput Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/io Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/os Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pathlib/Path Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pathlib/PosixPath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pathlib/PurePath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pathlib/PurePosixPath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pathlib/PureWindowsPath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pathlib/UnsupportedOperation Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pathlib/WindowsPath Linked here; links to this page; shared tags: #stdlib