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'