pathlib.PurePath
Everything that is pure string logic — parsing, joining, name and suffix, parents, matching — lives on PurePath. It never reads the disk, so it is safe for paths from another machine and for paths that do not exist.
Demo
from pathlib import PurePosixPath p = PurePosixPath('docs//guide/./intro.md/') (p, p.parts)
Normalisation is purely textual, so it only does what is safe without asking the OS: "//" becomes "/", "." segments and a trailing slash go away, but "a/../b" is not "b" (a could be a symlink). Exactly two leading slashes are kept because POSIX lets systems give "//" a special meaning; three or more collapse to one. The empty string means the current directory and prints as ".".
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| *pathsegments | str | os.PathLike | no ('.') | Segments joined like os.path.join: an absolute segment restarts the path. No segments means the current directory, shown as ".". |
Return value
PurePosixPath | PureWindowsPath — PurePath itself is never instantiated: it returns the flavour of the running OS.
Common patterns
from pathlib import PurePosixPath, PureWindowsPath remote = PurePosixPath('/var/log/app.log') share = PureWindowsPath(r'\\fileserver\reports\q3.xlsx')
import os from pathlib import Path def load(path: str | os.PathLike) -> str: return Path(path).read_text(encoding='utf-8')
from pathlib import PurePosixPath seen = {PurePosixPath('a/b'), PurePosixPath('a//b/')} len(seen) # 1
Examples
Pitfalls
from pathlib import PurePosixPath, PureWindowsPath PurePosixPath('a') < PureWindowsPath('a')
from pathlib import PurePosixPath PurePosixPath('a') < PurePosixPath('b')
from pathlib import PurePosixPath PurePosixPath('a/../b') == PurePosixPath('b')
from pathlib import Path Path('a').mkdir() Path('b').touch() Path('a/../b').samefile('b')
When to use
- Path arithmetic on paths that may not exist, or belong to another machine
- Code that must behave identically on Windows and Linux (choose PurePosixPath or PureWindowsPath)
- Tests: no file system needed
- Anything that touches the disk (exists, read_text, glob) → Path
- Resolving ".." or symlinks → Path.resolve()
Notes
FAQ
PurePath only manipulates the path text: joining, splitting, name, suffix, matching. Path is a subclass that adds I/O: exists, read_text, mkdir, glob and so on. Every Path is also a PurePath.