Python Glossary Русский

all Python 3osfunction

os.path.split

os.path — path parsing

Split a path into a (head, tail) pair — dirname and basename together.

Syntax

os.path.split(path)

It returns a pair that rebuilds the path: join(*split(p)) gives an equivalent path. With a trailing slash the name is empty: split('a/b/') returns ('a/b', ''). It splits at the last separator only; to break a path into all its components pathlib.Path(p).parts is handier.

Examples

import os
print(os.path.split('/a/b/c'))   # → ('/a/b', 'c')
path = '/home/user/file.txt'
print(os.path.split(path))  # → ('/home/user', 'file.txt')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy