all Python 3osfunction
os.path.basename
os.path — path parsing
Return the final component of a path (the file/dir name).
Syntax
os.path.basename(path)
For a path ending in a separator it returns an empty string: basename('a/b/') gives '', not 'b' — unlike the Unix basename utility. If a path may arrive with a trailing slash, strip it with rstrip('/') or normalise it with normpath. The function never touches the filesystem; it is pure string handling.
Examples
import os
print(os.path.basename('/a/b/c.txt')) # → c.txt
path = '/home/user/file.txt'
print(os.path.basename(path)) # → file.txt
print(repr(os.path.basename('/home/user/'))) # → '' — после последнего / пусто
See also
Open in the full glossarydocs.python.orgReport an inaccuracy