os.path.splitroot
splitroot (3.12+) is the primitive the other functions are built on: drive + root + tail == path, always. POSIX has no drives, a root of '/' (or exactly '//', which POSIX leaves implementation-defined). Windows drives are 'C:', '\\server\share' or device names like '\\?\C:'.
Demo
import posixpath, ntpath (posixpath.splitroot('/home/ada'), ntpath.splitroot('/home/ada'))
posixpath never finds a drive — 'C:\Users\ada' is a relative name to it. ntpath recognises 'C:' and a root of '\'; for '//server/share/docs/a.txt' the whole '//server/share' is the drive and the following '/' is the root (ntpath keeps your slash style). 'C:Users' has a drive but no root: relative to the current folder of drive C. splitdrive is splitroot with the root glued back onto the tail.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| path | str | bytes | PathLike | yes | Any path. |
Return value
tuple[str, str, str] | tuple[str, str] — Pieces that concatenate back to the original path.
Common patterns
import ntpath drive = ntpath.splitroot(p)[0] is_unc = drive[:2] in ('\\\\', '//')
import os drive, rest = os.path.splitdrive(path)
Examples
Pitfalls
[p[:2] for p in ['docs', r'\\srv\share\x']]
import ntpath [ntpath.splitdrive(p)[0] for p in ['docs', r'\\srv\share\x']]
import ntpath bool(ntpath.splitdrive('C:Users')[0])
import ntpath drive, root, tail = ntpath.splitroot('C:Users') bool(drive and root)
When to use
- Taking a path apart into drive, root and the rest
- Code that handles Windows paths on any OS (via ntpath)
- pathlib code → PurePath.drive, .root, .anchor
- Just "is it absolute?" → isabs
Notes
FAQ
os.path.splitdrive(path)[0] on Windows ('C:'). Anywhere, for Windows-style paths: ntpath.splitdrive(path)[0]. On POSIX the drive is always ''.