Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use .name attribute of the Path object.

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading