How to extract file extension using Python?

PythonServer Side ProgrammingProgramming

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')
raja
Published on 27-Dec-2017 11:23:16
Advertisements