Skip to main content

pathlib

Filesystem paths as objects. Pure paths are inert string operations; concrete paths add I/O. The classes split along OS conventions (PosixPath vs WindowsPath).

Source-of-record: Lib/pathlib/, pathlib docs.

Class hierarchy

ClassOS checkI/O
PurePathbothno
PurePosixPathPOSIXno
PureWindowsPathWindowsno
Pathcurrentyes
PosixPathPOSIXyes
WindowsPathWindowsyes

Construction

ConstructorEffect
Path('a', 'b', 'c')Build from parts.
Path.cwd() / Path.home()Common locations.

Parts and components

AttributeMeaning
partsTuple of path components.
drive / root / anchorWindows drive, root, drive+root.
parents / parentIterable of parents / immediate parent.
name / stem / suffix / suffixesFinal component pieces.

Pure-path operations

MethodReturns
joinpath(*others) / __truediv__(other)New path.
match(pattern, *, case_sensitive=None)Glob-style match (3.12 adds case sensitivity).
full_match(pattern, *, case_sensitive=None) (3.13+)Whole-path match.
relative_to(*other, walk_up=False)Relative path.
is_relative_to(other)Predicate.
with_name(name) / with_stem(stem) / with_suffix(suffix)Functional updates.
with_segments(*pathsegments) (3.12+)Build a new path of the same class.
as_posix() / as_uri()Conversion.
is_absolute() / is_reserved()Predicates.

Concrete-path I/O

MethodEffect
stat(*, follow_symlinks=True) / lstat()os.stat_result.
exists(*, follow_symlinks=True)Predicate.
is_file() / is_dir() / is_symlink() / is_socket() / is_fifo() / is_block_device() / is_char_device() / is_mount() / is_junction()Predicates.
iterdir()Iterator of children.
glob(pattern, *, case_sensitive=None, recurse_symlinks=False)Glob children.
rglob(pattern, ...)Recursive glob.
walk(top_down=True, on_error=None, follow_symlinks=False) (3.12+)Recursive walk.
open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)Open file.
read_bytes() / write_bytes(b)Whole-file binary.
read_text(encoding=None, errors=None, newline=None) / write_text(text, ...)Text.
touch(mode=0o666, exist_ok=True)Create / update mtime.
mkdir(mode=0o777, parents=False, exist_ok=False)Create dir.
rmdir() / unlink(missing_ok=False)Remove.
rename(target) / replace(target)Move.
symlink_to(target, target_is_directory=False) / hardlink_to(target) (3.10+)Link.
readlink()Symlink target.
samefile(other)Predicate.
expanduser() / resolve(strict=False) / absolute()Path resolution.
chmod(mode, *, follow_symlinks=True) / lchmod(mode)Permissions.
owner() / group()POSIX ownership.

Globbing semantics

* does not cross path separators; ** does (single ** matches zero or more components). Hidden files starting with . are not matched unless the pattern starts with ..

Gopy status

AreaState
PurePath familyComplete.
Path I/OComplete.
glob / rglob / walkComplete.
with_segments, case-sensitive globComplete.

Reference

  • CPython 3.14: pathlib.
  • Lib/pathlib/.
  • module/pathlib/. gopy port.
  • PEP 428.