Join our newsletter for the latest updates.

Python Program to Extract Extension From the File Name

In this example, you will learn to extract extension from the file name.

To understand this example, you should have the knowledge of the following Python programming topics:

Example 1: Using splitext() method from os module

import os
file_details = os.path.splitext('/path/file.ext')


('/path/file', '.ext')

file_details is tuple with one item as the name of file along with the path and other is the extension of the file.

Example 2: Using pathlib module

import pathlib



Note: It works for python 3.4 and above.

Did you find this article helpful?