You can use Python os module os.path.splitext() method to get the filename and file extension in Python as shown in the below example –
1 2 3 4 5 6 |
import os filename, file_extension = os.path.splitext('/path/to/somefile.txt') print(file_extension) ## Returns .txt print(filename) ## Returns /path/to/somefile |