-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
Description
Bug report
Bug description:
Summary
pathlib.Path treats filenames that consist only of a dot-prefixed extension such as .txt, .png, .jpeg, etc. as files with no suffix, and assigns the entire string to .stem.
This behavior is surprising and inconsistent with common filesystem interpretations, where such names are generally understood as files with an extension and an empty basename.
Examples
from pathlib import Path
p = Path(".txt")
p.stem # ".txt"
p.suffix # "" <-- unexpected
p.suffixes # [] <-- unexpectedExpected Behavior
For a filename representing “extension-only” files:
.txt→stem = "",suffix = ".txt".png→stem = "",suffix = ".png"
This would align with:
- how most operating systems treat these names
- how editors, file explorers, MIME resolvers interpret them
- the intuitive model that
"suffix = extension"
Actual Behavior
pathlib interprets .txt as:
- basename =
.txt - extension = none
This leads to operations like:
Path(".png").with_stem("123") # → "123" (loses .png)behaving inconsistently, and breaks workflows that expect deterministic suffix logic.
Why This Matters
pathlib is often used specifically to avoid manual string parsing. However, its current handling of extension-only filenames creates surprising situations when writing tools such as:
- batch image / asset processors
- file renamers
- templated filename builders
- any system relying on predictable suffix logic
This behavior is especially problematic on Windows, where .txt, .png, .ini are widely recognized as “files with an extension”.
Related Issues (Not duplicates)
I reviewed the following issues before submitting:
- pathlib .suffix, .suffixes, .stem unexpected behavior for pathname with trailing dot #82805 – behavior for filenames ending with a trailing dot (
"file.") - Suffix in pathlib is not behaving like a file extension #121347 – inconsistent suffix extraction with unusual patterns
These are related in spirit (edge cases in suffix/stem parsing), but do not cover the case where the entire filename is just an extension-like string such as:
.txt.png.jpeg
These are interpreted as “no suffix” and .stem == filename,
therefore this is not a duplicate.
Versions Tested
I have reproduced this behavior on:
- Python 3.11
- Python 3.12
I could not find any indication in the changelogs or issue tracker that this behavior has been changed in newer versions (3.13+). If this has already been addressed, a pointer would be appreciated.
Suggested Direction
I am not proposing an immediate breaking change, but raising the question:
Should extension-only filenames be handled in a more intuitive and consistent way,
aligning.suffixwith OS / filetool expectations?
Possible directions include:
- treating
.txtlike a normal extension - or at minimum documenting this edge case more clearly
Thank you
I appreciate the work that goes into pathlib, and I hope this issue helps improve the clarity and consistency of filename handling.
CPython versions tested on:
3.12
Operating systems tested on:
Windows