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 OverflowPython
docs.python.org › 3 › library › pathlib.html
pathlib — Object-oriented filesystem paths
February 23, 2026 - >>> PurePosixPath('my/library.tar.gz').stem 'library.tar' >>> PurePosixPath('my/library.tar').stem 'library' >>> PurePosixPath('my/library').stem 'library' Changed in version 3.14: A single dot (”.”) is considered a valid suffix. ... Return whether the path is absolute or not.
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')
python - Pathlib and stem - Attributerror - Stack Overflow
path.with_stem() was introduced in Python 3.9. More on stackoverflow.com
`pathlib.PurePath.with_stem('')` promotes file extension to stem
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 ... More on github.com
pathlib .suffix, .suffixes, .stem unexpected behavior for pathname with trailing dot
3.14bugs and security fixesbugs ... Library Python modules in the Lib/ directorytopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error ... Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state. ... assignee = None closed_at = None created_at = labels = ['3.8', 'type-bug', 'library'] title = 'pathlib .suffix, .suffixes, .stem unexpected ... More on github.com
add setter for pathlib.stem
Feature or enhancement Proposal: It would be nice to have the option to modify the stem of a file name directly. Say I have a Path '/tmp/myfile.npy': file_path = Path('/tmp/myfile.npy... More on github.com
Videos
34:51
Python Tutorial: Pathlib - The Modern Way to Handle File Paths ...
00:54
Python 3 | Path Modifications made easy with Pathlib! #coding ...
09:57
The Right Way to Handle File Paths in Python: pathlib.Path Explained ...
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 ...
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" {
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.
Mopicmp
mopicmp.github.io › python-pathlib › path-stem.html
Path.stem - Python Pathlib | BetterDocs
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.
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
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.
GitHub
github.com › python › cpython › issues › 82805
pathlib .suffix, .suffixes, .stem unexpected behavior for pathname with trailing dot · Issue #82805 · python/cpython
October 28, 2019 - 3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error ... Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state. ... assignee = None closed_at = None created_at = <Date 2019-10-28.22:54:26.232> labels = ['3.8', 'type-bug', 'library'] title = 'pathlib .suffix, .suffixes, .stem unexpected behavior for pathname with trailing dot' updated_at = <Date 2019-10-29.01:59:29.319> user = 'https://bugs.python.org/inyeollee'
Author inyeollee
Stack Abuse
stackabuse.com › introduction-to-the-python-pathlib-module
Introduction to the Python Pathlib Module
August 7, 2023 - stem: final path component without its suffix · suffix: the file extension of the final component · anchor: the part of a path before the directory. / is used to create child paths and mimics the behavior of os.path.join. joinpath: combines the path with the arguments provided · match(pattern): ...
Astral
docs.astral.sh › ruff › rules › os-path-splitext
os-path-splitext (PTH122) | Ruff
from pathlib import Path path = Path("foo/bar.py") root = path.parent / path.stem ext = path.suffix · While using pathlib can improve the readability and type safety of your code, it can be less performant than the lower-level alternatives that work directly with strings, especially on older versions of Python.
Python Tutorial
pythontutorial.net › home › python standard library › python path
Python Path: Interact with File System Using Path from pathlib
March 27, 2025 - from pathlib import Path path = Path('readme.txt') print(path.name)Code language: Python (python) Output: readme.txtCode language: Python (python) To get the file name without extension, you use the stem property: from pathlib import Path path = Path('readme.txt') print(path.stem)Code language: ...
GitHub
github.com › python › cpython › issues › 126537
add setter for pathlib.stem · Issue #126537 · python/cpython
November 7, 2024 - It would be nice to have the option to modify the stem of a file name directly. ... if the file exists, I want to change the file name to '/tmp/myfile-1.npy', in order to avoid overwriting it, I currently need to do the following: if file_path.exists(): file_path = file_path.parents / (file_path.stem + '-1' + file_path.suffix)
Author thawn