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.

os.path functionPython 3.0+ (Windows rule changed in 3.13)Live demo
Common call
os.path.isabs(path)
Returns
True / False
Replaces
path.startswith('/') — wrong on Windows
Watch out
'~/x' is not absolute (expanduser first); 'C:x' is not absolute on Windows
os.path.isabs(pathpath — Any path.type: str | bytes | PathLike · required)
→ bool

Demo

Live evaluation
Each path through posixpath.isabs and ntpath.isabs (Python 3.13 rules).
Try:
Inputs
pathslist[str]paths, comma-separated
Code
import posixpath, ntpath
[(p, posixpath.isabs(p), ntpath.isabs(p)) for p in ['/etc/hosts', 'C:\\Windows', 'C:Windows', 'docs']]
Result
[('/etc/hosts', True, False), ('C:\\Windows', False, True), ('C:Windows', False, False), ('docs', False, False)]

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

NameTypeRequiredDescription
pathstr | bytes | PathLikeyesAny path.

Return value

bool — True for an absolute path. The file system is not consulted.

Common patterns

Resolve relative config paths against a base
Leave absolute paths alone.
import os
if not os.path.isabs(path):
    path = os.path.join(BASE_DIR, path)
Require an absolute path argument
Fail early with a clear message.
import os
if not os.path.isabs(target):
    raise ValueError(f'expected an absolute path, got {target!r}')

Examples

1. POSIX: a leading slash
import posixpath (posixpath.isabs('/etc/hosts'), posixpath.isabs('etc/hosts'))
Returns
(True, False)
2. Windows: drive plus root
import ntpath (ntpath.isabs(r'C:\Windows'), ntpath.isabs('C:Windows'))
Returns
(True, False)
3. Windows: UNC share
import ntpath ntpath.isabs(r'\\server\share\x')
Returns
True
4. Windows 3.13+: no drive, not absolute
import ntpath ntpath.isabs('/etc')
Returns
False
5. A tilde is just a character
import posixpath posixpath.isabs('~/notes')
Returns
False
6. abspath always is
import os os.path.isabs(os.path.abspath('x'))
Returns
True

Pitfalls

1. Testing startswith('/') yourself
That is the POSIX rule only; it misses C:\ paths and accepts drive-less roots on Windows.
startswith('/')
[p.startswith('/') for p in [r'C:\Users', '/tmp']]
[False, True]
ntpath.isabs (Windows rules)
import ntpath
[ntpath.isabs(p) for p in [r'C:\Users', '/tmp']]
[True, False]
2. Forgetting to expand ~
isabs does not know about home directories. Expand first, then test.
isabs('~/x')
import posixpath
posixpath.isabs('~/data')
False
expanduser first
import os, posixpath
from unittest import mock
with mock.patch.dict(os.environ, {'HOME': '/home/ada'}):
    result = posixpath.isabs(posixpath.expanduser('~/data'))
result
True

When to use

Use it
  • Deciding whether to join a path onto a base folder
Reach for something else
  • pathlib code → PurePath.is_absolute()
  • Checking existence → os.path.exists

Notes

CPython impl
posixpath: s.startswith('/'). ntpath (3.13): take the first 3 characters with / turned into \, then test for ':\' at index 1 or a leading '\\'
Changed in 3.13
On Windows, returns False for a path that starts with exactly one (back)slash

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).