get File Extensions in Python

Understanding how to get File Extensions in Python

5 minutes read


Python is among the most powerful and adaptable programming languages available. It has a vast array of libraries and modules to make a wide range of tasks easier. Dealing with files and extracting data, like file extensions, is a challenge that developers face frequently. We will look at several approaches to Get file extension in Python in this post.

get file extension in python

Basics of File Extensions

It is essential to comprehend file extensions and their significance before attempting to work with Python programming. An extension (such as “.txt” or “.png”) is a suffix appended to the end of a filename, usually separated by a period. It provides crucial information about how the data within the file gets interpreted. Thereby acting as a means of identifying the file type or format.

Getting the file extension in Python is necessary for a number of operations, including sorting, validation, and file manipulation. Let’s look at a few ways to accomplish this in a Python setting.

A Like Blog – How to Use Quotient in Google Sheets 

Method 1: Using the split() Method

The split() method, which Python offers for strings, is one simple approach to extract the file extension. You can divide a string into substrings using the split() method by providing a delimiter. The period (.) that separates the filename and the extension in the context of file paths serves as the delimiter.

Here’s a sample code for it:

def get_file_extension(file_path):

    # Splits file path into components

    components = file_path.split(‘.’)

    # The last component is the file extension

    file_extension = components[-1]

    return file_extension

Using a period as a delimiter, this method splits a file path as input and returns the last component. The last component stands for the file extension. This approach has its limits, though, particularly if the filename has more than one period.

Method 2: Using the os.path.splitext() Method

The splitext() method in the Python os.path module offers a more reliable alternative. Its especially meant to split a file path into its root and extension. When you enter a file path, this method returns a tuple with the root and extension.

import os

def get_file_extension(file_path):

    # Use os.path.splitext() to split the file path

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

    # The file_extension variable now contains the extension with the leading period

    return file_extension

This approach has the advantage of handling situations in which the filename contains more than one period. It guarantees that the file extension gets correctly recognized as final segment of the file path following the final period.

Method 3: Using the pathlib Module

The pathlib module, which was first released in Python 3.4, offers an object-oriented method for manipulating file system paths. This module’s Path class makes it possible to do practical file path operations, such as getting the file extension.

from pathlib import Path

def get_file_extension(file_path):

    # Create a Path object

    path = Path(file_path)

    # Use suffix property for getting the file extension

    file_extension = path.suffix

    return file_extension

The Path object’s suffix attribute gives back the file extension together with the leading period. This approach gets used in Python 3 contexts since its thought as more understandable and contemporary.

get file extension in python

Method 4: Using Regular Expressions

In situations when you need further customization or pattern matching, file extension gets extracted using regular expressions, or regex. Python’s re module offers utilities for interacting with regular expressions.

import re

def get_file_extension(file_path):

    # Define a regex pattern to match the file extension

    pattern = re.compile(r’\.([a-zA-Z0-9]+)$’)

    # Use the search method to find the pattern in the file path

    match =

    if match:

        # The group(1) method retrieves the matched file extension

        file_extension =

        return file_extension


        # Returns None if no match is found out.

        return None

Although this approach can handle more intricate matching patterns, it could be unnecessary in straightforward situations. Although regular expressions are more powerful, they have a higher learning curve.

Read More Blog – How to Clear App Cache


We investigated several approaches to get file extensions in Python, accommodating varying needs and tastes. The decision relies on the particular requirements of your project. Whether you choose the flexibility of regular expressions, the elegance of pathlib, the robustness of os.path.splitext(), or the simplicity of string splitting.

Frequently asked questions

Q1: How should one go about getting the file extension in Python from a filename?

Ans: os.path.splitext() function is frequently used to obtain the file extension. However,  it may not perform as intended if the filename does not contain the entire path.

Q2: In Python, how can I find out if a file has a particular extension?

Ans: You can compare the extracted extension with the desired extension to see if a file has the desired extension.

Q3: Which extension is applied to files in Python?

Ans: Python files have the following extensions: .py,.pyi.

Leave a Reply

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