Trouble getting file name with extension using pathlib

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

>Solution :

You can use .name attribute of the Path object.

>>> from pathlib import Path
>>>
>>> p = Path("SomePath/filename.txt")
>>> p.name
'filename.txt'

Leave a Reply