You can extract the file extension of a filename string using the os.path.splitext method. It splits the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period.
example,
import os file_name = 'my_file.txt' print(os.path.splitext(file_name))
Output
You will get the output −
('my_file', '.txt')