pathlib.PosixPath
You rarely name these classes: they are what Path(...) returns. PosixPath = Path + PurePosixPath rules; WindowsPath = Path + PureWindowsPath rules. Only the one matching the OS can be created.
Common call
Path('x') # PosixPath('x') on Linux, WindowsPath('x') on Windows
Returns
the concrete class for the running OS
Replaces
—
Watch out
The foreign class raises UnsupportedOperation (NotImplementedError before 3.13)
pathlib.PosixPath(*pathsegments) / pathlib.WindowsPath(*pathsegments)
→ PosixPath | WindowsPath
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| *pathsegments | str | os.PathLike | no ('.') | Same as Path. |
Return value
PosixPath | WindowsPath — A concrete path for the running OS; the foreign class raises UnsupportedOperation.
Common patterns
Type-check against Path, not the concrete class
isinstance(p, Path) is True on every OS; isinstance(p, PosixPath) is not.
from pathlib import Path def ensure_path(x): return x if isinstance(x, Path) else Path(x)
Branch on the OS explicitly
When behaviour must differ, test os.name rather than the path class.
import os from pathlib import Path config = Path.home() / ('AppData/Roaming/app' if os.name == 'nt' else '.config/app')
Examples
1. Path returns the class for this OS
import os
from pathlib import Path, PosixPath, WindowsPath
type(Path('x')) is (WindowsPath if os.name == 'nt' else PosixPath)
Returns
True2. Each is a Path and a pure flavour
from pathlib import Path, PosixPath, PurePosixPath, WindowsPath, PureWindowsPath
(issubclass(PosixPath, Path), issubclass(PosixPath, PurePosixPath), issubclass(WindowsPath, PureWindowsPath))
Returns
(True, True, True)3. The foreign class cannot be created
import os
from pathlib import PosixPath, WindowsPath, UnsupportedOperation
foreign = PosixPath if os.name == 'nt' else WindowsPath
try:
foreign('x')
except UnsupportedOperation:
result = 'UnsupportedOperation'
result
Returns
'UnsupportedOperation'4. Which is also a NotImplementedError
from pathlib import UnsupportedOperation
issubclass(UnsupportedOperation, NotImplementedError)
Returns
True5. Equality needs the same flavour
from pathlib import Path, PurePosixPath, PureWindowsPath
Path('a') in (PurePosixPath('a'), PureWindowsPath('a'))
Returns
TruePitfalls
1. Checking isinstance(p, PosixPath)
The check is OS-dependent: it fails for every path on Windows. Check against Path (or PurePath) instead.
concrete class
import os from pathlib import Path, PosixPath isinstance(Path('x'), PosixPath) == (os.name != 'nt')
True
Path
from pathlib import Path isinstance(Path('x'), Path)
True
2. Hard-coding a repr in tests
repr(Path('x')) is PosixPath('x') on Linux but WindowsPath('x') on Windows. Compare with as_posix() or with a Path you build, not with a repr string.
repr string
import os from pathlib import Path repr(Path('a/b')) == ("WindowsPath('a/b')" if os.name == 'nt' else "PosixPath('a/b')")
True
as_posix()
from pathlib import Path Path('a/b').as_posix() == 'a/b'
True
When to use
Use it
- Reading tracebacks and reprs — these are the names you will see
- Very rarely, isinstance checks in OS-specific code
Reach for something else
- Creating paths → use Path(...), which picks the right class
- Foreign-OS paths → PurePosixPath / PureWindowsPath
Notes
CPython impl
Lib/pathlib/_local.py: class PosixPath(Path, PurePosixPath) and class WindowsPath(Path, PureWindowsPath); on the wrong OS their __new__ raises UnsupportedOperation("cannot instantiate 'PosixPath' on your system")
Changed in 3.13
The error is UnsupportedOperation; earlier versions raised NotImplementedError (still caught by except NotImplementedError, since UnsupportedOperation subclasses it)
Windows-only / POSIX-only methods
owner() and group() raise UnsupportedOperation on Windows (no pwd / grp); is_junction() is only ever True on Windows
FAQ
Path is a factory. On Windows Path(...) returns a WindowsPath, on Linux and macOS a PosixPath. Both are subclasses of Path, so isinstance(p, Path) is True either way.