How to Get File Extension in Python?

When I was working on a project, I had to process hundreds of files stored in a shared folder. The challenge was simple: I needed to identify file extensions before performing any operation.

At first, I thought Python would have an easy function for it. However, I soon realized that there are multiple ways to obtain file extensions in Python.

In this tutorial, I’ll share the three methods I use most often. I’ll also explain them, so even if you’re just starting with Python, you can follow along.

Why Do We Need to Get File Extensions in Python?

When working with files, the extension tells us what type of file we’re dealing with. For example, .csv files usually contain data tables, .jpg files are images, and .pdf files hold documents.

If you’re building an automation script in Python, checking the file extension helps you decide how to process the file.

Method 1 – Use Python os.path.splitext()

The first and most common method I use is the os.path.splitext() function. This function splits the file path into two parts: the filename and the extension.

import os

# Example file path
file_name = "C:/Users/John/Documents/report.pdf"

# Split the file name and extension
root, extension = os.path.splitext(file_name)

print("Root:", root)
print("Extension:", extension)

I executed the above example code and added the screenshot below.

python get file type

When I run this code, it gives me the root part of the file and the extension separately. This is my go-to method because it’s simple, reliable, and works across different operating systems.

Method 2 – Use Python pathlib Module

Another way I often use is the pathlib module. The Path object in Python has a .suffix property that directly gives you the file extension.

from pathlib import Path

# Example file path
file_path = Path("C:/Users/John/Documents/data.csv")

# Get the extension
extension = file_path.suffix

print("Extension:", extension)

I executed the above example code and added the screenshot below.

get file type python

This method is cleaner and more modern compared to os.path. I prefer pathlib when I’m already working with paths in my Python project.

Method 3 – Use Python String split() Method

Sometimes, I don’t want to import extra modules. In those cases, I simply use Python’s string split() method.

# Example file path
file_name = "C:/Users/John/Documents/image.jpeg"

# Split by dot and get the last part
extension = file_name.split(".")[-1]

print("Extension:", extension)

I executed the above example code and added the screenshot below.

python file extension

However, I don’t recommend it for production code because it doesn’t handle edge cases like files without extensions or hidden files starting with a dot.

Handle Multiple Extensions in Python

There are times when files have multiple extensions, like archive.tar.gz. In such cases, os.path.splitext() will only give you .gz. If you want all extensions, pathlib provides a property called .suffixes.

from pathlib import Path

# Example file with multiple extensions
file_path = Path("C:/Users/John/Downloads/archive.tar.gz")

# Get all suffixes
extensions = file_path.suffixes

print("Extensions:", extensions)

This gives me a list of all extensions: [‘.tar’, ‘.gz’]. It’s very useful when working with compressed files or backups.

Real-Life Example: Filter Files by Extension in Python

Let me show you how I’ve used this in a real project. I had a folder full of mixed files – PDFs, images, and spreadsheets. I only wanted to process the Excel files.

Here’s how I did it with Python:

import os

# Folder path
folder_path = "C:/Users/John/Documents/Projects"

# Loop through all files in the folder
for file in os.listdir(folder_path):
    root, extension = os.path.splitext(file)

    # Check if the extension is .xlsx
    if extension == ".xlsx":
        print("Found Excel file:", file)

This script helped me quickly filter out only the Excel files. It saved me hours of manual checking.

Compare the Three Python Methods

Here’s a quick comparison of the methods I’ve shown so far.

MethodModule NeededBest For
os.path.splitext()osGeneral use, simple cases
pathlib.suffixpathlibModern code, multiple extensions
split()NoneQuick scripts, not recommended for production

From my experience, I use os.path.splitext() for quick tasks and pathlib when working on larger projects.

Things to Keep in Mind

While getting file extensions in Python is easy, here are some tips I’ve learned over the years:

  • Always check if the file actually has an extension.
  • Be careful with hidden files (like .gitignore) that start with a dot.
  • Use pathlib.suffixes for files with multiple extensions.
  • Normalize extensions to lowercase if you’re comparing them.

Example: Normalize Extensions in Python

Sometimes, I get files with uppercase extensions like REPORT.PDF. To make sure my script doesn’t miss them, I always convert extensions to lowercase.

import os

file_name = "C:/Users/John/Documents/REPORT.PDF"

# Get extension and convert to lowercase
_, extension = os.path.splitext(file_name)
extension = extension.lower()

print("Normalized Extension:", extension)

I executed the above example code and added the screenshot below.

python get file extension

This way: .PDF and .pdf are treated the same.

Bonus: Check File Types with Python mimetypes

Apart from extensions, Python also has a mimetypes module. It guesses the file type based on the extension.

import mimetypes

file_name = "C:/Users/John/Documents/report.pdf"

# Guess the MIME type
mime_type, _ = mimetypes.guess_type(file_name)

print("MIME Type:", mime_type)

This is useful when you need to know not just the extension but also the file type.

So these are the different ways you can get file extensions in Python. I showed you how to use os.path.splitext(), pathlib, and the string split() method. I also shared how to handle multiple extensions and normalize them.

From my 10+ years of experience, I can say that pathlib is the most flexible method, while os.path.splitext() is the most commonly used.

Next time you’re writing a Python script to process files, try one of these methods. It will save you time and make your code cleaner.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.