Home > Enterprise >  Trouble getting file name with extension using pathlib
Trouble getting file name with extension using pathlib

Time:11-04

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

CodePudding user response:

You can use .name attribute of the Path object.

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