os.path.isabs
posixpath.isabs is s.startswith('/'). ntpath.isabs wants 'X:\' (or 'X:/') or a leading double slash. Since Python 3.13, '\Windows' — rooted but without a drive — is no longer absolute on Windows, because it depends on the current drive.
Demo
import posixpath, ntpath [(p, posixpath.isabs(p), ntpath.isabs(p)) for p in ['/etc/hosts', 'C:\\Windows', 'C:Windows', 'docs']]
Only POSIX thinks '/etc/hosts' is absolute: on Windows 3.13+ it is rooted on the current drive, so ntpath.isabs says False (Python 3.12 and earlier said True). 'C:\Windows' is absolute on Windows only; 'C:Windows' means "Windows in the current folder of drive C" and is relative everywhere. UNC paths count as absolute on Windows in either slash style, and '~/notes' is relative until expanduser replaces the tilde.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| path | str | bytes | PathLike | yes | Any path. |
Return value
bool — True for an absolute path. The file system is not consulted.
Common patterns
import os if not os.path.isabs(path): path = os.path.join(BASE_DIR, path)
import os if not os.path.isabs(target): raise ValueError(f'expected an absolute path, got {target!r}')
Examples
Pitfalls
[p.startswith('/') for p in [r'C:\Users', '/tmp']]
import ntpath [ntpath.isabs(p) for p in [r'C:\Users', '/tmp']]
import posixpath posixpath.isabs('~/data')
import os, posixpath from unittest import mock with mock.patch.dict(os.environ, {'HOME': '/home/ada'}): result = posixpath.isabs(posixpath.expanduser('~/data')) result
When to use
- Deciding whether to join a path onto a base folder
- pathlib code → PurePath.is_absolute()
- Checking existence → os.path.exists
Notes
FAQ
os.path.isabs(path), or Path(path).is_absolute(). On POSIX it means a leading '/'; on Windows a drive with a root (C:\) or a UNC path (\\server\share).