How to Get File Extension in Python

This article shows four methods to get the file extension in Python.

Getting File Extension in Python
Getting File Extension in Python

File manipulation in programming is crucial. It involves identifying the type and format of the file. This makes it possible to rename files, change their extension names, etc. The use case possibilities are endless.

In this article, you will learn four methods to get and extract file extensions in Python.

Let's get started.

Using the os.path module

The os.path module is a part of the Python standard library and provides a way to interact with the file system. os.path gives us the splitext() function that is particularly useful for separating a file path into its root and extension components.

Let's see this in action

import os

def get_file_extension(filepath):
    _, extension = os.path.splitext(filepath)
    return extension

# Example usage
file_path = "D:\GEEKBITS\__name__.png"
extension = get_file_extension(file_path)
print(f"The file extension is: {extension}")

In this example, we import the os module which provides us with different functions. We are particularly interested in the splitext() function.

We then define a function(get_file_extension(filepath)) that takes in a file path as an argument and returns the extension. Inside the function, we utilize the os.path.splitext()function to split the file path into the root and extension.

Notice the variables used to store the processed root and extension. The extension is captured by the extension variable and is used as the return value while we use an underscore ("_") as a throwaway variable (not needed) to store the root part.

Below the defined function, we give the file_path to the file then call the get_file_extension(file_path) function with the file_path as the input parameter. The last thing is to print the extension in the console.

Output:

Img 1.png

Using the Path from pathlib module

The pathlib module was introduced in Python 3.4. It provides an object-oriented interface for file system paths. With this method, you can use the suffix attribute of the Path class to extract the file extension.

Let's see it in action.

from pathlib import Path

def get_file_extension(filepath):
    path = Path(filepath)
    return path.suffix

# Example usage
file_path = "D:\GEEKBITS\__name__.png"
extension = get_file_extension(file_path)
print(f"The file extension is: {extension}")

In this example, we import the Path class from the pathlib module.

The next step is to define a function that returns the path.suffix. This function takes the filepath as an argument.

Inside the function, we use the Path() constructor to create a Path object from the given filepath.

We then access the suffix attribute of the Path object to get the file extension which is used as the return value for the defined function.

Below the function, we provide the file path and store the return value of the function inside the extension variable. The extension variable is then printed on the console.

Output

Img 2.png

Using the split() method

You can use a simpler approach to separate the filename from the extension using the dot(.) character and the split() method.

Here is how it works. we create a function that takes in the filepath as an argument. The split() method is then called on the filepath string. Inside the split() method, we provide the separator which is a dot (".") in this case.

Let's see it in action.

def get_file_extension(filepath):
    return filepath.split(".")[-1]

# Example usage
file_path = "D:\GEEKBITS\__name__.png"
extension = get_file_extension(file_path)
print(f"The file extension is: {extension}")

Notice the array indexing [-1] at the end of the split(). This is used to select the last element of the list which is assumed to be the file extension.

Below the function, we provide the file_path and save the return value of the function into the extension variable. Which is then printed onto the console.

Output:

Img 3.png

Using the Regular expressions (regex)

Regular expressions provide more control over file extensions. It is more complex but very versatile. Let's see it in action.

import re

def get_file_extension(filepath):
    match = re.search(r'\.([^.]+)$', filepath)
    if match:
        return match.group(1)
    else:
        return None

# Example usage
file_path = "D:\GEEKBITS\__name__.png"
extension = get_file_extension(file_path)
print(f"The file extension is: {extension}")

We import the re module and create a function that takes in the filepath as an argument.

inside the function, we employ the (re.search) method to search for a pattern within the filepath. The pattern is a regular expression that captures the part of the string after the last dot(.) in the file path. The capture pattern is stored in the match variable.

We then use the if statement to see if there is a match. If a match is found, we return the group in the regex. in this case, it is the file extension.

Below the function, we provide the filepath and print the value returned after the function is called.

Output:

Img 4.png

Let's explain the regex in detail.

\.:

  • The backslash (\) is an escape character in regular expressions.
  • The dot (.) is a special character in regex that matches any character except a newline.
  • Together, \. matches a literal dot character.

([^.]+):

  • The parentheses ( ... ) create a capturing group.
  • [^.] is a character class that matches any character except a dot.
  • + means one or more occurrences of the preceding character class.
  • So, ([^.]+) captures one or more characters that are not dots.

$`:

  • $ asserts the position at the end of the string.

Putting it all together, the regular expression is saying:

  • \.: Match a literal dot.
  • ([^.]+): Capture one or more characters that are not dots.
  • $: Assert that this pattern occurs at the end of the string.

Those are four methods to get file extensions in Python.

Summing Up

Using the methods above, you can easily get a file extension and use it as you please. You can use any of the methods. This article has gone deep into explaining the methods in detail so that you understand what you are doing.

Thank you for reading : )

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.