PurePath.is_absolute

A POSIX path is absolute when it starts with '/'. A Windows path needs a drive AND a root ('C:\x' or '\\server\share\x'). is_reserved() is deprecated since 3.13 in favour of os.path.isreserved().

pathlib methodPython 3.4+Live demo
Common call
p.is_absolute()
Returns
bool
Replaces
os.path.isabs(p)
Watch out
PureWindowsPath('/x') is not absolute
PurePath.is_absolute() / .is_reserved()
→ bool

Demo

Live evaluation
Absolute on POSIX means a root is present.
Try:
Inputs
pathstra path
Code
from pathlib import PurePosixPath
p = PurePosixPath('/etc/hosts')
(p.is_absolute(), p.root)
Result
(True, '/')

'~/notes.txt' is relative: pathlib never expands '~' on its own — Path.expanduser() does that. A path with two leading slashes keeps '//' as its root and is absolute.

Common patterns

Make a path absolute only if needed
absolute() prefixes the cwd; it is a no-op for absolute paths.
from pathlib import Path
p = Path(user_value)
if not p.is_absolute():
    p = Path.cwd() / p
Reject reserved Windows names (3.13+)
os.path.isreserved replaces the deprecated is_reserved().
import os
if os.name == 'nt' and os.path.isreserved(filename):
    raise ValueError(f'{filename} is a reserved device name')

Examples

1. Rooted POSIX path
from pathlib import PurePosixPath PurePosixPath('/usr/bin').is_absolute()
Returns
True
2. Relative POSIX path
from pathlib import PurePosixPath PurePosixPath('usr/bin').is_absolute()
Returns
False
3. Windows: drive + root
from pathlib import PureWindowsPath PureWindowsPath('C:/Windows').is_absolute()
Returns
True
4. Windows: root without drive
from pathlib import PureWindowsPath PureWindowsPath('/Windows').is_absolute()
Returns
False
5. Windows: drive without root
from pathlib import PureWindowsPath PureWindowsPath('C:Windows').is_absolute()
Returns
False
6. cwd() is always absolute
from pathlib import Path Path.cwd().is_absolute()
Returns
True
7. is_reserved (deprecated)
import warnings from pathlib import PurePosixPath, PureWindowsPath with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) result = (PureWindowsPath('nul').is_reserved(), PurePosixPath('nul').is_reserved()) result
Returns
(True, False)

Pitfalls

1. Treating "~" as absolute
The tilde is an ordinary name to pathlib until you call expanduser().
raw ~
from pathlib import PurePosixPath
PurePosixPath('~/data').is_absolute()
False
expanduser() first
from pathlib import Path
Path('~/data').expanduser().is_absolute()
True
2. Assuming a leading slash is absolute on Windows
'\temp' is relative to the current drive on Windows. Code that checks p.root instead of is_absolute() gets this wrong.
check root
from pathlib import PureWindowsPath
bool(PureWindowsPath('/temp').root)
True
is_absolute()
from pathlib import PureWindowsPath
PureWindowsPath('/temp').is_absolute()
False

When to use

Use it
  • Validating config values that must be absolute
  • Deciding whether to join a path onto a base folder
Reach for something else
  • Making a path absolute → Path.absolute() or resolve()
  • New code checking device names → os.path.isreserved() (Windows, 3.13+)

Notes

CPython impl
POSIX flavour: True if any raw segment starts with "/"; Windows flavour: ntpath.isabs (drive and root required)
is_reserved
Deprecated in 3.13, scheduled for removal in 3.15 — emits DeprecationWarning; always False for POSIX paths
Changed in 3.13
is_reserved also flags Windows names containing a colon or ending with a dot or space

FAQ

Path(p).is_absolute() — or os.path.isabs(p). On Windows both require a drive and a root.