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)

Output

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)

Output

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))

Output

File extension  ['.tar', '.gz']
The given File Extension is :  .tar.gz
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Type()

Python type()

Table of Contents Hide type() Syntaxtype() Parameterstype() Return ValueDifference between type() function and isinstance() functionExample of isinstance() method in PythonExample 1: Finding the type of a Python objectExample 2: type()…
View Post
Check If List Is Empty Python

Check if list is empty in Python

Table of Contents Hide  How to Check if List is Empty in PythonSolution 1: Using PEP 8 recommended Style.Solution 2: Using the bool() methodSolution 3: Using the len() method.Solution 4:…
View Post
Numpy Median

NumPy median

Table of Contents Hide What is a Median?numpy.median() Syntaxnumpy.median() Parametersnumpy.median() Return ValueExample 1: Calculate the median of the array using numpy.median() methodExample 2: Calculate the median of the array as…
View Post