Python Glossary Русский

all Python 3osfunction

os.path.splitdrive

os.path — path parsing

Split a path into a (drive, rest) pair; the drive is empty on non-Windows.

Syntax

os.path.splitdrive(path)

It matters for code handling Windows paths: on Unix the drive is always empty. A UNC network path counts as a whole drive: ntpath.splitdrive('//server/share/x') returns ('//server/share', '/x'). Note that 'C:x' is relative to the current directory of drive C, not to its root — splitroot makes that visible.

Examples

import os
print(os.path.splitdrive('/a/b'))   # → ('', '/a/b')
print(os.path.splitdrive('C:\\Users\\ivan'))   # → только Windows: ('C:', '\\Users\\ivan'); на Unix диск не выделяется — ('', 'C:\\Users\\ivan')
drive, rest = os.path.splitdrive('/tmp/data.csv')
print(drive + rest)   # → /tmp/data.csv — склейка частей всегда восстанавливает исходный путь
print(os.path.splitdrive(''))   # → ('', '')
# Python 3.12+
import os
print(os.path.splitroot('/a/b'))   # → ('', '/', 'a/b') — splitroot дополнительно отделяет корень, splitdrive оставляет его в остатке

See also

Open in the full glossarydocs.python.orgReport an inaccuracy