Skip to content

Path(".png").stem should return "" and not file extension like".png" #142009

@tokitoki22

Description

@tokitoki22

Feature or enhancement

Proposal:

I am using " translation tool" to write this post, but the content itself is not machine-generated. Everything described here is based on my actual experience, testing, and verification in Python just to make custom nodes for comfy-ui . I reported this issue as pathlib bug, but one contributor set the post as "invalid "and "not thinking"because I use LLM to format report and transltate,

but the reason "I use LLM" was simply make report precise and clear. Then Now I report again as "suggesition".

When a filename like ".png" or ".txt" is passed to a Path object and I use fp.stem or try to rename the file using fp.with_stem(), I believe the returned values should follow what the documentation describes. Currently the behavior is inconsistent for extension-only filenames.

This is actual code segment, (for custom node to save file with free prefix suffix, mix use preset json path names, for comfy-ui

fp = Path(file_name)
f_stem = pre_exp + fp.stem + suf_exp
save_file = fp.with_stem(f_stem)

In this custom save txt/image node, user can set "prefix" , "suffix" with file name, and dir path. there are case, user may only hope to set file name as ".png" or ".txt" , but use prefix to batch generation.
(set seed int values as string list prefix , or set Time, date as string suffix , with empty filename input, but "file identifier" like ".png" , ".jpeg" etc.

python pathlib document
pathlib.stem
"The final path component, without its suffix"

so, If {file_name} is ".png" or ".txt" , I expect fp.stem should return "", then
f_stem >> pre_exp + suf_exp

finally, fp will change user input "file name" by new stems(eg "298562", but keep their file type. (.txt or .png) ,
by using with_stem()

But actual behavor was
fp = Path(".png")
fp >> WindowsPath('.png')

fp.stem >> ".png" (it is not "stem" but it return file identifier, or suffix)
fp.with_stem("11539") >> "11539" (it remove the identiifer ".png" and convert it with stem.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

#142005 (comment)
btw now my report seems open again, thanks.

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