The Wayback Machine - https://web.archive.org/web/20240204103727/https://www.geeksforgeeks.org/python-os-path-splitext-method/
Open In App
Related Articles

Python | os.path.splitext() method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common pathname manipulation.

os.path.splitext() method in Python is used to split the path name into a pair root and ext. Here, ext stands for extension and has the extension portion of the specified path while root is everything except ext part.
ext is empty if specified path does not have any extension. If the specified path has leading period (‘.’), it will be ignored.

For example consider the following path names:

      path name                          root                        ext
/home/User/Desktop/file.txt    /home/User/Desktop/file              .txt
/home/User/Desktop             /home/User/Desktop                  {empty}
file.py                               file                          .py
.txt                                  .txt                         {empty}   

Syntax: os.path.splitext(path)

Parameter:
path: A path-like object representing a file system path. A path-like object is either a str or bytes object representing a path.

Return Type: This method returns a tuple that represents root and ext part of the specified path name.

Code: Use of os.path.splitext() method




# Python program to explain os.path.splitext() method 
    
# importing os module 
import os
  
# path
path = '/home/User/Desktop/file.txt'
  
# Split the path in 
# root and ext pair
root_ext = os.path.splitext(path)
  
# print root and ext
# of the specified path
print("root part of '% s':" % path, root_ext[0])
print("ext part of '% s':" % path, root_ext[1], "\n")
  
  
# path
path = '/home/User/Desktop/'
  
# Split the path in 
# root and ext pair
root_ext = os.path.splitext(path)
  
# print root and ext
# of the specified path
print("root part of '% s':" % path, root_ext[0])
print("ext part of '% s':" % path, root_ext[1])


Output:

root part of '/home/User/Desktop/file.txt': /home/User/Desktop/file
ext part of '/home/User/Desktop/file.txt': .txt 

root part of '/home/User/Desktop/': /home/User/Desktop/
ext part of '/home/User/Desktop/': 

Reference: https://docs.python.org/3/library/os.path.html

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 22 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials