Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Get File Extension in Python

  • 13 Aug, 2025
  • Com 0
How to Get a File Extension in Python

The most recommended way to get a file extension in Python is to split the filename into a tuple (root, extension) using os.path.splitext() method.

Get the extension of a file in Python

A file extension is the suffix at the end of a filename (e.g., .txt, .jpg, .pdf) that suggests the file’s format.

import os

filename = "vmm.pdf"

root, ext = os.path.splitext(filename)

print(ext)

# Output: '.pdf'

In this code, we are splitting the input file’s name into a tuple of two elements:

  1. filename
  2. extension

We are unpacking a tuple directly and printing the extension, which is .pdf.

Often, the filename is a file path, meaning the filename string looks like this: “/home/user/docs/vmm.pdf”.

Without a leading dot (.)

Without a leading dot (.) in Python

What if I want a file extension but without a dot (.)? In the above code example, the output is with (.). Use the lstrip() method to remove the dot.

import os

filename = "vmm.pdf"

ext = os.path.splitext(filename)[1].lstrip('.')

print(ext)

# Output: pdf

In the above code, I explicitly specify the tuple’s second element, which returns .pdf, and then apply the lstrip() method to remove the dot, returning just the extension.

File without extension

File without extension

There will be a scenario where the file won’t have any extension. It will be just like this “mbb”.

In that case, the os.path.splitext() method returns an empty string since there is no extension to fetch.

import os

file_path = "/home/user/docs/mbb"

root, ext = os.path.splitext(file_path)

print(ext)

# Output: ""

Multiple Dots (Compound Extensions)

What if the filename or file path contains multiple dots (.) like this: “backup.tar.gz”

In that case, it will return the last extension, which is .gz

import os

file_path = 'backup.tar.gz'

root, ext = os.path.splitext(file_path)

print(ext)

# Output: .gz

Files starting with Dot (.)

If you are working with Git, you will encounter a .gitignore file. Now, that file has no extension, and it starts with a .(dot). What happens if you pass this file to the os.path.splitext() method? Let’s find out.

import os

path = '.gitignore'

root, ext = os.path.splitext(path)

print(ext)

# Output: ""

As expected, it returns an empty string because there is no extension, and if the dot (.) appears at the start, the remaining name is not an extension.

Directories (No Extension Expected)

What about a path that does not have a file, just a directory? Well, again, since there is no file, there is no extension, and the output will be an empty string.

import os

path = '/usr/local/bin/'

root, ext = os.path.splitext(path)

print(ext)

# Output: "" (ignores trailing slash)

Modern alternative

If you are using Python 3.4+, you can use the pathlib.Path().suffix to get the extension of a file.

It is more object-oriented and works well with a modern language. The Path(path).suffix returns the extension, including the dot.

from pathlib import Path

path = Path('hogwarts.txt')

print(path.suffix)

# Output: '.txt'

To remove the dot (.) from the output string, use the path.suffix.lstrip(‘.’) method.

from pathlib import Path

path = Path('hogwarts.txt')

print(path.suffix.lstrip('.'))

# Output: txt

That’s all!

Post Views: 42
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Skip a Value in Python List
How to Swap Two Elements in a List in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend