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')
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 ...
13:52
Pathlib makes file management EASY in Python - YouTube
00:54
Python 3 | Path Modifications made easy with Pathlib! #coding ...
Python
docs.python.org › 3 › library › pathlib.html
pathlib — Object-oriented filesystem paths
To access the individual “parts” (components) of a path, use the following property: ... >>> p = PurePath('/usr/bin/python3') >>> p.parts ('/', 'usr', 'bin', 'python3') >>> p = PureWindowsPath('c:/Program Files/PSF') >>> p.parts ('c:\\', 'Program Files', 'PSF') (note how the drive and local root are regrouped in a single part) The parent, name, stem, and suffix properties describe different parts of a path.
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.
NHS Digital
nhsdigital.github.io › rap-community-of-practice › training_resources › python › handling-file-paths
Handling file paths - RAP Community of Practice
August 21, 2026 - # Note: __file__ is a global Python variable this_file_path = pathlib.Path(__file__) print("Path to file:", this_file_path) print("File name:", this_file_path.name) print("File stem:", this_file_path.stem) print("Parent folder:", this_file_path.parent) To see all the options (there are many!) use help(pathlib.Path) or see the pathlib-docs.
Basicexamples
basicexamples.com › example › python › pathlib-purepath-stem
Basic example of Python function pathlib.PurePath.stem
python · pathlib · purepath · from pathlib import PurePath path = PurePath("/path/to/file.txt") stem = path.stem print(stem) path manipulation · python · pathlib · purepath · file handling · python · pathlib · purepath · file handling ·
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): ...
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)