How to Get File Extension from File Name in Python?

Extracting the file extension from a file name is a common task in Python, especially when working with file processing or automation scripts. File extensions help in identifying the file type and can be crucial for handling files appropriately. In this article, we’ll look at different ways to extract the file extension from a file name in Python.

Method 1: Using os.path.splitext()

The os module is one of the most commonly used modules for file manipulation in Python. The os.path.splitext() function can be used to split the file name into two parts: the name and the extension.

import os

file_name = "example.txt"
name, extension = os.path.splitext(file_name)

print(extension)

In this example, os.path.splitext() splits the file name into a tuple, where the first element is the base name, and the second element is the extension.

You can see the output when I run the code in the editor:

Get File Extension from File Name in Python
Get File Extension from File Name in Python

Method 2: Using pathlib.Path()

pathlib is a modern library introduced in Python 3 for handling file paths in a more intuitive way? The Path class from pathlib has a suffix property that can be used to get the file extension.

from pathlib import Path

file_name = "example.docx"
file_path = Path(file_name)
extension = file_path.suffix

print(extension)  # Output: .txt

Here, we create a Path object and then use its suffix property to get the file extension. This method is often considered more Pythonic and is recommended in modern Python code.

Once you run the code, you can see it will give the extension as .docx like the below screenshot.

Python Get File Extension from File Name
Python Get File Extension from File Name

Read: How to compare two lists in Python

Method 3: Using string splitting

While this method is not as robust as the ones mentioned earlier, it’s still worth mentioning that you can also use basic string manipulation to get the file extension. You can use the split() function and extract the last part of the string as the extension.

file_name = "example.txt"
extension = file_name.split('.')[-1]

print(extension)  # Output: txt

Please note that this method does not include the dot (.) in the extension. Also, if the file name does not have an extension, this method will not behave as expected, so it’s less reliable compared to the other methods.

Handling Edge Cases

It’s important to handle edge cases like file names without extensions or file names that start with a dot.

import os

file_name = "file_without_extension"
_, extension = os.path.splitext(file_name)
print(extension)  # Output: ""

file_name = ".hiddenfile"
_, extension = os.path.splitext(file_name)
print(extension)  # Output: ""

Both the os.path.splitext() and pathlib.Path().suffix methods handle these edge cases well and will return an empty string if there’s no extension.

Conclusion

Extracting file extensions from file names in Python is simple and can be done using various methods in Python. The os.path.splitext() and pathlib.Path().suffix methods are the most robust and recommended for this task. Although string splitting can be used, it’s not as reliable and should be used with caution, especially in production code.

You may also like: