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.

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.

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.

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.
| Method | Module Needed | Best For |
|---|---|---|
| os.path.splitext() | os | General use, simple cases |
| pathlib.suffix | pathlib | Modern code, multiple extensions |
| split() | None | Quick 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.suffixesfor 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.

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:
- Append Elements to a Tuple in Python
- Return a Tuple in Python
- Python Set vs Tuple
- Unpack a Tuple in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.