Python Program to Extract Extension From the File Name – 2024

Hey guys, in this blog we will see a Python Program to Extract Extension From the File Name.

Table of Contents

Example 1: Using splitext() method from the os module

import os
file_details = os.path.splitext('/path/file.exe')
print(file_details)
print(file_details[1])

Output

exe

  • os.path.splitext() will return two things; first will be the whole path without extension and second will be the extension.
  • file_details[1] will have the extension.

Example 2: Using pathlib module

import pathlib
print(pathlib.Path('/path/file.exe').suffix)

Output

exe

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 520

Subscribe to our Newsletter

Subscribe to our newsletter and receive all latest projects...

Leave a Reply

Your email address will not be published. Required fields are marked *