- How to get file extension in Python?
- Python get file extension using os module splitext() function
- Python get file extension using pathlib module
- How to Get File Extension in Python
- Getting File Extension in Python
- Get File Extension using Pathlib Module
- Conclusion
- Python: Get a File’s Extension (Windows, Mac, and Linux)
- Using Python Pathlib to Get a File’s Extension
- How to get file extension python (4 ways)
- Method 1 – Using os.path.splitext() to get file extension Python
- Method 2 – Using pathlib.Path().suffix to get file extension Python
- Method 3 – Using split() to get file extension Python
- Method 4 – Using regex to get file extension Python
- Conclusion
How to get file extension in Python?
In Python, we can extract the file extension using two approaches. Let’s take a look at each of these with examples.
Python get file extension using os module splitext() function
The os module has extensive functions for interacting with the operating system. The OS module can be used to easily create, modify, delete, and fetch file contents or directories.
Syntax: os.path.splitext(path)
The function splitext() will take the path as an argument and return the tuple with filename and extension.
import os # returns tuple wit filename and extension file_details = os.path.splitext('/home/usr/sample.txt') print("File Details ",file_details) # extract the file name and extension file_name = file_details[0] file_extension = file_details[1] print("File Name: ", file_name) print("File Extension: ", file_extension)
File Details ('/home/usr/sample', '.txt') File Name: /home/usr/sample File Extension: .txt
Python get file extension using pathlib module
pathlib module comes as a standard utility module in Python and offers classes representing filesystem paths with semantics appropriate for different operating systems.
pathlib.path().suffix method can be used to extract the extension of the given file path.
import pathlib # pathlib function which returns the file extension file_extension = pathlib.Path('/home/usr/sample.txt').suffix print("The given File Extension is : ", file_extension)
The given File Extension is : .txt
What if your extension is like sample.tar.gz with multiple dots, and if you use the above methods, you will only get the last part of the extension, not the full extension.
You can use the pathlib module with suffixes property which returns all the extensions as a list. Using that, we can join into a single string, as shown below.
import pathlib # pathlib function which returns the file extension file_extension = pathlib.Path('/home/usr/sample.tar.gz').suffixes print("File extension ", file_extension) print("The given File Extension is : ", ''.join(file_extension))
File extension ['.tar', '.gz'] The given File Extension is : .tar.gz
How to Get File Extension in Python
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
We can use Python os module splitext() function to get the file extension. This function splits the file path into a tuple having two values — root and extension.
Getting File Extension in Python
import os # unpacking the tuple file_name, file_extension = os.path.splitext("/Users/pankaj/abc.txt") print(file_name) print(file_extension) print(os.path.splitext("/Users/pankaj/.bashrc")) print(os.path.splitext("/Users/pankaj/a.b/image.png"))
Output:
- In the first example, we are directly unpacking the tuple values to the two variables.
- Note that the .bashrc file has no extension. The dot is added to the file name to make it a hidden file.
- In the third example, there is a dot in the directory name.
Get File Extension using Pathlib Module
We can also use pathlib module to get the file extension. This module was introduced in Python 3.4 release.
>>> import pathlib >>> pathlib.Path("/Users/pankaj/abc.txt").suffix '.txt' >>> pathlib.Path("/Users/pankaj/.bashrc").suffix '' >>> pathlib.Path("/Users/pankaj/.bashrc") PosixPath('/Users/pankaj/.bashrc') >>> pathlib.Path("/Users/pankaj/a.b/abc.jpg").suffix '.jpg' >>>
Conclusion
It’s always better to use the standard methods to get the file extension. If you are already using the os module, then use the splitext() method. For the object-oriented approach, use the pathlib module.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Python: Get a File’s Extension (Windows, Mac, and Linux)
In this tutorial, you’ll learn how to use Python to get a file extension. You’ll accomplish this using both the pathlib library and the os.path module.
Being able to work with files in Python in an easy manner is one of the languages greatest strength. You could, for example use the glob library to iterate over files in a folder. When you do this, knowing what the file extension of each file may drive further decisions. Because of this, knowing how to get a file’s extension is an import skill! Let’s get started learning how to use Python to get a file’s extension, in Windows, Mac, and Linux!
The Quick Answer: Use Pathlib
Using Python Pathlib to Get a File’s Extension
The Python pathlib library makes it incredibly easy to work with and manipulate paths. Because of this, it makes perfect sense that the library would have the way of accessing a file’s extension.
The pathlib library comes with a class named Path , which we use to create path-based objects. When we load our file’s path into a Path object, we can access specific attributes about the object by using its built-in properties.
Let’s see how we can use the pathlib library in Python to get a file’s extension:
# Get a file's extension using pathlib import pathlib file_path = "/Users/datagy/Desktop/Important Spreadsheet.xlsx" extension = pathlib.Path(file_path).suffix print(extension) # Returns: .xlsx
We can see here that we passed a file’s path into the Path class, creating a Path object. After we did this, we can access different attributes, including the .suffix attribute. When we assigned this to a variable named extension , we printed it, getting .xlsx back.
This method works well for both Mac and Linux computers. When you’re working with Windows, however, the file paths operate a little differently.
Because of this, when using Windows, create your file path as a “raw” string. But how do you do this? Simply prefix your string with a r , like this r’some string’ . This will let Python know to not use the backslashes as escape characters.
Now that we’ve taken a look at how to use pathlib in Python to get a file extension, let’s explore how we can do the same using the os.path module.
Want to learn more? Want to learn how to use the pathlib library to automatically rename files in Python? Check out my in-depth tutorial and video on Towards Data Science!
How to get file extension python (4 ways)
In this tutorial, we will be solving a program for how to get file extension in python. We will be solving the above problem statement in 4 ways.
Method 1 – Using os.path.splitext() to get file extension Python
The os.path is a pre-defined python module that has utility functions to manipulate OS file paths. These functions are used for opening, retrieving, saving, and updating information from the file path. To know more about os.path module and its function you read this.
To get file extension in Python we can use splitext() method present in the os.path module. The splittext() method returns a tuple that contains the root file path and extension of the file.
Python code
# importing os.path import os.path file_name = "projects/project_name/specs.txt" #using splitext() file_root = os.path.splitext(file_path)[0] file_extension = os.path.splitext(file_path)[1] print("Root Path of file:", file_root) print("File Extension:", file_extension)
Root Path of file: projects/project_name/specs File Extension: .txt
Method 2 – Using pathlib.Path().suffix to get file extension Python
pathlib.Path().suffix method of Pathlib module is used to retrieve file extension from the given input file path.
pathlib.Path().suffix method takes file name as input and returns the extension of the file including . at the beginning.
import pathlib file_name = "projects/project_name/specs.txt" # using pathlib.Path().suffix file_extension = pathlib.Path(file_name).suffix print("File extension:", file_extension)
Method 3 – Using split() to get file extension Python
We can get the file extension by using split() method. By using split() method we can file extension without .
file_name = "projects/project_name/specs.txt" # using split file_extension = file_name.split(".")[-1] print("File extension:", file_extension)
Method 4 – Using regex to get file extension Python
We can get file extension in python by applying regex expression on the file path.
import re file_name = "projects/project_name/specs.txt" # using regex file_extension = re.search(r"\.([^.]+)$", file_name).group(1) print("File extension:", file_extension)
Conclusion
Hence by using os.path.splitext(), pathlib.Path().suffix, regex, or split() method we can get file extension in python.
I am Passionate Computer Engineer. Writing articles about programming problems and concepts allows me to follow my passion for programming and helping others.