How to Get File Extension from Filename in Python?

Published On: 12/10/2022 | Category: Python


Hi Guys,

Today, python get file extension from path is our main topic. if you want to see example of python get filename extension from path then you are a right place. In this article, we will implement a how to get file extension in python. I’m going to show you about python get file extension from filename.

I'll give you a straightforward illustration of how to obtain the file extension from the file path in this case. let's examine a straightforward example with output.

You can use these examples with python3 (Python 3) version.

So let's see bellow example:

Example:

main.py
import os
  
fileName, fileExtension = os.path.splitext('demo.png')
  
print("File Name: ", fileName)
print("File Extension: ", fileExtension)
Output
File Name:  demo
File Extension:  .png

It will help you....

Happy Pythonic Coding!