Trouble getting file name with extension using pathlib

Question:

I have this code which gives me the filename no problem. It gives it to me without the extension. Any way to get with extension?

from pathlib import Path

file = 'somepath'
path_object = Path(file)
filename = path_object.stem
Asked By: UneRoue

||

Answers:

You can use .name attribute of the Path object.

>>> from pathlib import Path
>>>
>>> p = Path("SomePath/filename.txt")
>>> p.name
'filename.txt'
Answered By: Abdul Niyas P M
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.