Skip to content

pathlib: Filenames consisting only of an extension (".txt", ".png") are treated as having no suffix #142005

@tokitoki22

Description

@tokitoki22

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  # []        <-- unexpected

Expected Behavior

For a filename representing “extension-only” files:

  • .txtstem = "", suffix = ".txt"
  • .pngstem = "", 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:

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 .suffix with OS / filetool expectations?

Possible directions include:

  • treating .txt like 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-pathlibtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions