Answers for "python path get filename with extension"

6

how to get just the filename in python

import os
>>> base=os.path.basename('/root/dir/sub/file.ext')
>>> base
'file.ext'
>>> os.path.splitext(base)
('file', '.ext')
>>> os.path.splitext(base)[0]
'file'
Posted by: Guest on May-19-2020
2

python get file extension from path

import os.path
extension = os.path.splitext(filename)[1]
Posted by: Guest on May-08-2020
0

pathlib path get filename with extension

>>> from pathlib import Path
>>> p = Path("/home/user/Downloads/repo/test.txt")
>>> print(p.stem)
test
>>> print(p.name)
test.txt
Posted by: Guest on May-20-2021

Code answers related to "python path get filename with extension"

Python Answers by Framework

Browse Popular Code Answers by Language