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
Updated on 18-Feb-2020 06:09:25

Advertisements