Skip to content

All topics  /  Python

How to Get File Extension from Filename in Python?

Python

Sep 20, 2022

Hi Dev,

In this tutorial, I will show you How to Get File Extension from Filename in Python. you will learn How to Get File Extension From Path Python. you can see How to Get File Extension From Name Python. I explained simply about How to Get File Extension in Python. you will do the following things for Python Get Filename Extension from Path.

In this example, I will give you a simple example of getting the file extension from file path. so let's see a simple example with output.

so let's see following examples with output:

Example


main.py

import os

  
fileName, fileExtension = os.path.splitext('/images/demo.png')

  
print("File Name: ", fileName)
print("File Extension: ", fileExtension)

Output:

File Name:  /images/demo
File Extension:  .png