Python os.path.splitext() Method
- 
          
            Syntax of Python 
os.path.splitext()Method - 
          
            Example 1: Use the 
os.path.splitext()Method in Python - 
          
            Example 2: If There Is No 
extPart inos.path.splitext()Method - 
          
            Example 3: Shorter Syntax of the 
os.path.splitext()Method - 
          
            Example 4: Concatenate the 
rootandextPart in theos.path.splitext()Method - 
          
            Example 5: Create a Different File Extension Using the 
os.path.splitext()Method 
Python os.path.splitext() method is an efficient way to split a path name into a pair of root and ext. The ext means extension, which has the extension part of the specified path, while the root part is everything except the ext part.
Syntax of Python os.path.splitext() Method
os.path.splitext(path)
Parameters
path | 
an address object of a file system path or a symlink. The object can either be an str or bytes | 
Return
The return type of this method is a tuple representing the ext and root parts of the specified path name.
Example 1: Use the os.path.splitext() Method in Python
import os
path = "home/user/File.txt"
split = os.path.splitext(path)
print("The root part of '% s' is:" % path, split[0])
print("The ext part of '% s' is:" % path, split[1])
Output:
The root part of 'home/user/File.txt' is: home/user/File
The ext part of 'home/user/File.txt' is: .txt
As seen in the above code, the first element of the tuple is the root, and the subsequent one is the ext component.
Example 2: If There Is No ext Part in os.path.splitext() Method
import os
path = "home/user/Desktop"
split = os.path.splitext(path)
print("The root part of '% s' is:" % path, split[0])
print("The ext part of '% s' is:" % path, split[1])
Output:
The root part of 'home/user/Desktop' is: home/user/Desktop
The ext part of 'home/user/Desktop' is:
The os.path.splitext() splits the text at the right-most period. The ext part is empty if there is no period in the file address.
Example 3: Shorter Syntax of the os.path.splitext() Method
    
import os
print(os.path.splitext("temp.bar.exe"))
Output:
('temp.bar', '.exe')
The above example shows that the file path’s leading periods are considered part of the root.
Example 4: Concatenate the root and ext Part in the os.path.splitext() Method
import os
path = "/home/user/Desktop/File.txt"
root, ext = os.path.splitext(path)
print("The root part is: ", root)
print("The ext part is: ", ext)
path_address = root + ext
print("The full path address is: ", path_address)
Output:
The root part is:  /home/user/Desktop/File
The ext part is:  .txt
The full path address is:  /home/user/Desktop/File.txt
In any case, concatenating the root and ext components will always make the original path.
Example 5: Create a Different File Extension Using the os.path.splitext() Method
import os
path = "/home/user/Desktop/File.txt"
root, ext = os.path.splitext(path)
print("The root part is: ", root)
print("The ext part is: ", ext)
path_address = root + ".exe"
print("The full path address is: ", path_address)
Output:
The root part is:  /home/user/Desktop/File
The ext part is:  .txt
The full path address is:  /home/user/Desktop/File.exe
Using the above code, you can easily have a new file address with a new name.
Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.
LinkedIn