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')
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
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
Practical code examples showing how Path.stem is used in real projects.
PyTutorial
pytutorial.com › python-pathlibstem-explained-file-path-manipulation
PyTutorial | Python Pathlib.stem Explained | File Path Manipulation
March 23, 2025 - For example, you can use with_suffix() to change the file extension after extracting the stem. ... from pathlib import Path # Create a Path object file_path = Path("/documents/report.txt") # Change the suffix new_path = file_path.with_suffix(".pdf") print(new_path) ... This code changes the file extension from .txt to .pdf. Learn more about Python Pathlib with_suffix().
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: ...
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" {
ZetCode
zetcode.com › python › pathlib
Python pathlib - working with files and directories in Python with pathlib
$ parts2.py The stem is: wordpress-5.1.tar The name is: wordpress-5.1.tar.gz The suffix is: .gz The anchor is: C:\ File name: wordpress-5.1 The suffixes: ['.1', '.tar', '.gz'] The iterdir yields path objects of the directory contents. ... #!/usr/bin/python from pathlib import Path path = Path('C:/Users/Jano/Documents') dirs = [e for e in path.iterdir() if e.is_dir()] print(dirs)
Runebook.dev
runebook.dev › en › docs › python › library › pathlib › pathlib.PurePath.stem
python - Beyond the Extension: A Guide to pathlib.stem
import os from pathlib import Path multi_dot_file = 'archive.tar.gz' # pathlib method p = Path(multi_dot_file) print(f"pathlib stem: {p.stem}") # archive.tar # os.path method name_part, ext_part = os.path.splitext(multi_dot_file) print(f"os.path name part: {name_part}") # archive.tar print(f"os.path extension part: {ext_part}") # .gz · Observation In this specific case (archive.tar.gz), both methods behave similarly by only stripping the last extension. The main benefit of pathlib is its object-oriented structure and platform-independent logic. For modern Python code, the best practice is to use pathlib exclusively for path manipulation, which avoids these interoperability issues and makes your code cleaner and more robust across different operating systems.
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
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.
GitHub
github.com › python › cpython › issues › 114610
`pathlib.PurePath.with_stem('')` promotes file extension to stem · Issue #114610 · python/cpython
January 26, 2024 - Bug report >>> pathlib.PurePath('foo.jpg').with_stem('').stem '.jpg' The with_stem() call should raise ValueError, as it's impossible for a filename to have an empty stem and a non-empty suffix. Linked PRs gh-114612 gh-114613
Author barneygale