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:
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.
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:
- How to get file name from a path in Python?
- How to read a CSV file in Python
- Save image to file in Python
- Python write variable to file
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.