Python: get file extension
The easiest way to get a file extension in python is splitext('filename')
method from os
module. This method returns a tuple that contains two values: filename (without extension) and its extension.
import os
file = "your/path/to/file.py"
file2 = "another/path/to/file.html"
file3 = "file.txt"
print(os.path.splitext(file))
print(os.path.splitext(file2))
print(os.path.splitext(file3))
Output:
('your/path/to/file', '.py')
('another/path/to/file', '.html')
('file', '.txt')