HOWTO · Python

How to Get File Extension in Python

It demonstrates how to get the file extension in Python.

On this page

To get a filename’s extension in Python, use os.path.splitext() for the final suffix or pathlib.Path.suffixes when you need multiple suffix parts, such as .tar.bz2. The examples below show how each result is structured so you can choose the form your code needs.

Use the os.path Module to Extract Extension From File in Python

os.path provides utility functions for working with operating-system paths. Its splitext() function returns a two-item tuple: the root path and the final extension. Assign the tuple in that order, even if you name the variables differently.

Run this example with a path ending in .docx:

import os

path = "/Users/user/Documents/sampledoc.docx"
root, extension = os.path.splitext(path)

print("Root:", root)
print("Extension:", extension)
Root: /Users/user/Documents/sampledoc
Extension: .docx

The output shows the root path first and the extension second. splitext() considers only the final suffix: for a name such as backup.tar.gz, it separates .gz and leaves backup.tar in the root.

The extension returned by splitext() includes its leading dot, which is convenient for comparisons such as extension == ".csv". If the filename has no suffix, the extension part is an empty string and the root remains the filename. A leading dot by itself does not count as a suffix, so a name like .profile is treated as having no extension by this rule.

Use the pathlib Module to Extract Extension From File in Python

pathlib represents paths with objects. Construct a Path from a path string, then inspect its attributes: parent is the containing path, name is the final path component, and suffix is the final suffix including its leading dot.

For a single-suffix file such as sampledoc.docx, path.suffix is .docx. The attributes are values, so read them without parentheses. This standalone example also shows the difference between suffix and suffixes:

import pathlib

single = pathlib.Path("/Users/user/Documents/sampledoc.docx")
archive = pathlib.Path("/Users/user/Documents/app_sample.tar.bz2")

print("Parent:", single.parent)
print("Filename:", single.name)
print("Final suffix:", single.suffix)
print("Suffix parts:", single.suffixes)
print("Archive suffix:", "".join(archive.suffixes))
Parent: /Users/user/Documents
Filename: sampledoc.docx
Final suffix: .docx
Suffix parts: ['.docx']
Archive suffix: .tar.bz2

The output shows the containing path, final filename, final suffix, suffix list, and joined archive suffix. suffixes is a list: for sampledoc.docx, it contains one item, .docx; for app_sample.tar.bz2, it contains .tar and .bz2. Joining those parts with an empty string produces .tar.bz2. This is useful when the application treats a compound suffix as one unit, but many filenames have only one suffix.

When a path has no suffix, Path.suffix is an empty string and Path.suffixes is an empty list. A leading dot alone, as in .profile, is not returned as a suffix. Keep the list if later code needs to inspect each part; join it only when the application expects a single compound value. These attributes describe the path’s final name component, regardless of whether that path currently points to an existing file.

Choose os.path.splitext() when the final suffix is all you need or when you also need the root returned in the same operation. Choose Path.suffix for a single suffix with an object-oriented path, and Path.suffixes when you need the suffix components separately or joined as a compound value. Both approaches inspect the name; neither checks that a file exists.