Python 3.4+pathlibterm
pathlib.PurePath.stem
path components
The final component's name without its last suffix: 'report.tar' for 'report.tar.gz'.
Syntax
PurePath(...).stem
Only the last suffix is removed, so for archives like .tar.gz the stem still contains a dot. To strip all suffixes, take name and cut off ''.join(suffixes). with_suffix() changes the extension, and since 3.9 with_stem() changes the stem.
Examples
# Python 3.9+
from pathlib import PurePosixPath as P
p = P('/data/report.tar.gz')
print(p.stem) # → report.tar
print(p.name.removesuffix(''.join(p.suffixes))) # → report
print(p.with_stem('final')) # → /data/final.gz
See also
Open in the full glossarydocs.python.orgReport an inaccuracy