Python 3.4+pathlibterm
pathlib.PurePath.name
path components
The final path component in full — the file name with its extension: 'report.tar.gz'.
Syntax
PurePath(...).name
For a path ending in a directory, name is that directory's name; for the root '/' it is an empty string. The property only parses the path as a string and never touches the disk: the file need not exist. with_name() replaces it.
Examples
from pathlib import PurePosixPath as P
p = P('/data/report.tar.gz')
print(p.name) # → report.tar.gz
print(repr(P('/').name)) # → ''
print(p.with_name('summary.txt')) # → /data/summary.txt
See also
Open in the full glossarydocs.python.orgReport an inaccuracy