uncrayon
uncrayon

Reputation: 475

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

Upvotes: 1

Views: 700

Answers (1)

user459872
user459872

Reputation: 24562

You can use .name attribute of the Path object.

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

Upvotes: 2

Related Questions