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:'.

os.path functionPython 3.12+ (splitdrive: all versions)Live demo
Common call
drive, root, tail = os.path.splitroot(path)
Returns
('C:', '\\', 'Users\\ada')
Replaces
path[:2] tricks for drive letters
Watch out
posixpath: '//x' keeps both slashes as the root, '///x' only one
os.path.splitroot(path) → (drive, root, tail) / os.path.splitdrive(path) → (drive, rest)
→ tuple[str, str, str] | tuple[str, str]

Demo

Live evaluation
posixpath.splitroot and ntpath.splitroot side by side.
Try:
Inputs
pathstrany path
Code
import posixpath, ntpath
(posixpath.splitroot('/home/ada'), ntpath.splitroot('/home/ada'))
Result
(('', '/', 'home/ada'), ('', '/', '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

NameTypeRequiredDescription
pathstr | bytes | PathLikeyesAny path.

Return value

tuple[str, str, str] | tuple[str, str] — Pieces that concatenate back to the original path.

Common patterns

Is this a UNC path?
The drive of a UNC path starts with two separators.
import ntpath
drive = ntpath.splitroot(p)[0]
is_unc = drive[:2] in ('\\\\', '//')
Strip the drive for display
Keep root and tail.
import os
drive, rest = os.path.splitdrive(path)

Examples

1. POSIX: root and tail
import posixpath posixpath.splitroot('/home/ada')
Returns
('', '/', 'home/ada')
2. POSIX: exactly two slashes stay
import posixpath (posixpath.splitroot('//srv/x'), posixpath.splitroot('///srv/x'))
Returns
(('', '//', 'srv/x'), ('', '/', '//srv/x'))
3. Relative: no root
import posixpath posixpath.splitroot('docs/a.txt')
Returns
('', '', 'docs/a.txt')
4. Windows drive
import ntpath ntpath.splitroot(r'C:\Users\ada')
Returns
('C:', '\\', 'Users\\ada')
5. Windows UNC share
import ntpath ntpath.splitroot(r'\\server\share\docs\a.txt')
Returns
('\\\\server\\share', '\\', 'docs\\a.txt')
6. splitdrive
import ntpath ntpath.splitdrive(r'C:\Users\ada')
Returns
('C:', '\\Users\\ada')
7. POSIX has no drives
import posixpath posixpath.splitdrive('/home/ada')
Returns
('', '/home/ada')

Pitfalls

1. Slicing the drive letter by hand
p[:2] is not a drive for relative paths or UNC shares.
p[:2]
[p[:2] for p in ['docs', r'\\srv\share\x']]
['do', '\\\\']
splitdrive
import ntpath
[ntpath.splitdrive(p)[0] for p in ['docs', r'\\srv\share\x']]
['', '\\\\srv\\share']
2. Assuming a drive means absolute
'C:Users' has a drive but no root — it is relative to drive C's current folder.
drive only
import ntpath
bool(ntpath.splitdrive('C:Users')[0])
True
drive and root
import ntpath
drive, root, tail = ntpath.splitroot('C:Users')
bool(drive and root)
False

When to use

Use it
  • Taking a path apart into drive, root and the rest
  • Code that handles Windows paths on any OS (via ntpath)
Reach for something else
  • pathlib code → PurePath.drive, .root, .anchor
  • Just "is it absolute?" → isabs

Notes

CPython impl
Both modules have a C implementation (Modules/posixmodule.c) with a pure-Python fallback; splitdrive is built on splitroot
Added
splitroot in 3.12
POSIX //
IEEE Std 1003.1-2017, 4.13 Pathname Resolution: two leading slashes may be interpreted in an implementation-defined way, so posixpath keeps them; three or more mean the same as one

FAQ

os.path.splitdrive(path)[0] on Windows ('C:'). Anywhere, for Windows-style paths: ntpath.splitdrive(path)[0]. On POSIX the drive is always ''.