You could just .split it:
>>> Path('logs/date.log.txt').stem.split('.')[0]
'date'
os.path works just as well:
>>> os.path.basename('logs/date.log.txt').split('.')[0]
'date'
It passes all of the tests:
In [11]: all(Path(k).stem.split('.')[0] == v for k, v in {
....: 'a': 'a',
....: 'a.txt': 'a',
....: 'archive.tar.gz': 'archive',
....: 'directory/file': 'file',
....: 'd.x.y.z/f.a.b.c': 'f',
....: 'logs/date.log.txt': 'date'
....: }.items())
Out[11]: True
Answer from Blender on Stack Overflow Top answer 1 of 7
40
You could just .split it:
>>> Path('logs/date.log.txt').stem.split('.')[0]
'date'
os.path works just as well:
>>> os.path.basename('logs/date.log.txt').split('.')[0]
'date'
It passes all of the tests:
In [11]: all(Path(k).stem.split('.')[0] == v for k, v in {
....: 'a': 'a',
....: 'a.txt': 'a',
....: 'archive.tar.gz': 'archive',
....: 'directory/file': 'file',
....: 'd.x.y.z/f.a.b.c': 'f',
....: 'logs/date.log.txt': 'date'
....: }.items())
Out[11]: True
2 of 7
9
Why not go recursively?
from pathlib import Path
def true_stem(path):
stem = Path(path).stem
return stem if stem == path else true_stem(stem)
assert(true_stem('d.x.y.z/f.a.b.c') == 'f')
Videos
09:57
The Right Way to Handle File Paths in Python: pathlib.Path Explained ...
34:51
Python Tutorial: Pathlib - The Modern Way to Handle File Paths ...
00:54
Python 3 | Path Modifications made easy with Pathlib! #coding ...
16:56
A Deep Dive Into Pathlib And The Magic Behind It - YouTube
26:42
Introduction to File Paths and Python's pathlib: Python Basics ...
23:46
Python Standard Library: Pathlib - YouTube
Python.org
discuss.python.org βΊ ideas
Add pathlib.Path.stems - Ideas - Discussions on Python.org
April 20, 2023 - Path.suffix is to Path.stem as Path.suffixes is to β¦? >>> from pathlib import Path >>> p = Path("CurveFile.vs2022.vcxproj") >>> p.suffix '.vcxproj' >>> p.stem 'CurveFile.vs2022' >>> p.suffixes ['.vs2022', '.vcxproj'] While you can programmatically get each and every suffix, currently, there is no way to extract first part ('CurveFile') without doing string manipulation.
Mopicmp
mopicmp.github.io βΊ python-pathlib βΊ path-stem.html
Path.stem - Python Pathlib | BetterDocs
Example 1 From rht/eigen-neovim (eigen_neovim/github_client.py) Copy Β· for filepath in output_dir.glob("*.lua"): if filepath.name.endswith(".meta"): continue # Filename format: owner__name.lua stem = filepath.stem if "__" in stem: owner, name = stem.split("__", 1) cached.add(f"{owner}/{name}") return cached def fetch_configs( self, query: str = "filename:init.lua path:nvim", max_repos: int = 500, progress_callback=None, ) -> Iterator[ConfigFile]: """ Fetch Neovim configurations from GitHub.
Real Python
realpython.com βΊ python-pathlib
Python's pathlib Module: Taming the File System β Real Python
January 11, 2025 - Youβre using .with_stem() to create the new filename without changing the extension. The actual copying takes place in the highlighted line, where you use .read_bytes() to read the content of source and then write this content to destination using .write_bytes(). While itβs tempting to use pathlib for everything path related, you may also consider using shutil for copying files.
OneUptime
oneuptime.com βΊ home βΊ blog βΊ how to use pathlib for file paths in python
How to Use pathlib for File Paths in Python
January 27, 2026 - # path_components.py # Accessing different parts of a path from pathlib import Path path = Path("/home/user/project/data/report.csv.gz") # Name - final component print(f"Name: {path.name}") # report.csv.gz # Stem - name without final suffix print(f"Stem: {path.stem}") # report.csv # Suffix - final extension print(f"Suffix: {path.suffix}") # .gz # Suffixes - all extensions print(f"Suffixes: {path.suffixes}") # ['.csv', '.gz'] # Parent - immediate parent directory print(f"Parent: {path.parent}") # /home/user/project/data # Parents - all ancestor directories for parent in path.parents: print(f" {
IONOS
ionos.com βΊ digital guide βΊ websites βΊ web development βΊ python pathlib
How does Python pathlib work and what are its benefits?
June 18, 2025 - from pathlib import Path md_path = Path("/Users/name/folder/blue.md") txt_path = md_path.with_name("red.txt") md_path.replace(txt_path)python Β· Even if Path itself does not have a method for copying, you can use .with_stem() to duplicate a file. This creates a new file name without changing the file extension. This is an example of the apΒproΒpriΒate code:
Rednafi
rednafi.com βΊ home βΊ python βΊ no really, python's pathlib is great
No really, Python's pathlib is great | Redowan's Reflections
December 30, 2025 - Weβll linearly traverse through the tree and provide necessary examples to grasp their usage. Path β βββ Attributes β βββ parts β βββ parent & parents β βββ name β βββ suffix & suffixes β βββ stem β β βββ Methods βββ joinpath(*other) βββ cwd() βββ home() βββ exists() βββ expanduser() βββ glob() βββ rglob(pattern) βββ is_dir() βββ is_file() βββ is_absolute() βββ iterdir() βββ mkdir(mode=0o777, parents=False, exist_ok=False) βββ open(mode='r', buffering=-1, encoding=None, ...) βββ rename(target) βββ replace(target) βββ resolve(strict=False) βββ rmdir()
GitHub
github.com βΊ python βΊ cpython βΊ issues βΊ 114610
`pathlib.PurePath.with_stem('')` promotes file extension to stem Β· Issue #114610 Β· python/cpython
January 26, 2024 - 3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13bugs and security fixesbugs and security fixestopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error ... The with_stem() call should raise ValueError, as it's impossible for a filename to have an empty stem and a non-empty suffix.
Author Β barneygale
Iditect
iditect.com βΊ faq βΊ python βΊ clean-way-to-get-the-quottruequot-stem-of-a-path-object-in-python.html
Clean way to get the "true" stem of a Path object in python?
In Python, if you want to obtain the "true" stem of a Path object (the stem without any file extension), you can use the stem attribute provided by the pathlib module. The stem attribute returns the name of the file or directory without its suffix. ... from pathlib import Path # Example Path ...
Readthedocs
autoclasstoc.readthedocs.io βΊ en βΊ latest βΊ examples βΊ pathlib.html
pathlib.Path β autoclasstoc 1.7.0 documentation
The snippet below shows how autoclasstoc can be used to document the pathlib.Path class from the Python standard library. This example also includes inheritance.