We can get a file’s extension by using the pathlib module in Python 3.
1
2
3
4
5
6
7
8
9
10
11
12 | from pathlib import Path
filename = "/home/user/Desktop/MyImage.jpg"
file_path_object = Path(filename)
extension = file_path_object.suffix
filename_no_extension = file_path_object.stem
print(f"{extension=}, {file_no_extension=}")
>> extension='.jpg', file_no_extension='MyImage'
|