Python Glossary Русский

Python 3.12+osfunction

os.path.splitroot

os.path — path parsing

Split a path into a (drive, root, tail) triple (3.12+).

Syntax

os.path.splitroot(path)

It separates the root from the drive, which tells apart paths that splitdrive conflates: ntpath.splitroot('C:a') gives ('C:', '', 'a') — no root, so the path is relative. On POSIX exactly two leading slashes are kept as the root, while with three or more the root is a single slash and the rest stay in the tail unchanged: splitroot('//x') gives ('', '//', 'x'), and splitroot('///x') gives ('', '/', '//x'). Added in Python 3.12.

Examples

import os
print(os.path.splitroot('/a/b'))   # → ('', '/', 'a/b')
print(os.path.splitroot('a/b'))   # → ('', '', 'a/b') — пустой root означает относительный путь
drive, root, tail = os.path.splitroot('/a/b/c.txt')
print(drive + root + tail)   # → /a/b/c.txt — три части всегда склеиваются обратно в исходный путь
print(os.path.splitroot(''))   # → ('', '', '')
print(os.path.splitroot('C:/Users'))   # → только Windows: ('C:', '/', 'Users'); на Unix буква диска не выделяется — ('', '', 'C:/Users')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy