Python Program to Extract Extension From the File Name – 2022

Machine Learning Projects

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

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

Leave a Comment

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