×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Extracting extension from filename in Python

Last Updated : April 28, 2025

A file extension usually indicates the type of file, such as '.txt', '.jpg', or '.py'. In this chapter, we will explore multiple methods to extract the extension from a filename in Python.

Approach 1: Using os.path.splitext()

You can extract the file extension using the os.path.splitext() function, which splits the filename into a name and extension pair.

Example

This example demonstrates extracting the extension using os.path.splitext().

import os

def get_extension(filename):
    return os.path.splitext(filename)[1]

# Test cases
print(get_extension("document.txt"))
print(get_extension("archive.tar.gz"))

The output of the above code would be:

.txt
.gz

Approach 2: Using str.split() Method

You can split the filename by a dot (.) and pick the last part to get the extension.

Example

This example demonstrates using simple string splitting to retrieve the file extension.

def get_extension(filename):
    parts = filename.split('.')
    if len(parts) > 1:
        return '.' + parts[-1]
    return ''

# Test cases
print(get_extension("photo.jpg"))
print(get_extension("file"))        # (empty string)

The output of the above code would be:

.jpg

Approach 3: Using pathlib.Path

You can use the Path class from the pathlib module to easily access the suffix (extension) of a file.

Example

This example demonstrates using pathlib.Path to get the file extension.

from pathlib import Path

def get_extension(filename):
    return Path(filename).suffix

# Test cases
print(get_extension("report.pdf"))
print(get_extension("music.mp3"))
print(get_extension("compressed.zip"))

The output of the above code would be:

.pdf
.mp3
.zip

Approach 4: Using rpartition() Method

You can use the rpartition() method to split the filename at the last dot and obtain the extension.

Example

This example demonstrates using rpartition() to find and extract the extension.

def get_extension(filename):
    before_dot, sep, after_dot = filename.rpartition('.')
    if sep:  # Means a '.' was found
        return '.' + after_dot
    return ''

# Test cases
print(get_extension("presentation.pptx"))
print(get_extension("notes"))              # (empty string)

The output of the above code would be:

.pptx

Exercise

Select the correct option to complete each statement about extracting the extension from a filename in Python.

  1. The ___ module in Python provides utilities to work with file paths, including extracting file extensions.
  2. The function ___ from the os.path module can split the file name and extension.
  3. When using splitext(), the extension is returned as the ___ part of the result.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.