OS Path Splitext() Example
In this example, the path has no extensions therefore ext will be empty.
splitext('Coding Ninjas Studio')
('Coding Ninjas Studio', '')
In this example, the path contains an extension therefore ext=.exe (including the leading period). The previous period will be ignored.
splitext('Coding Ninjas Studio.study.exe')
('Coding Ninjas Studio.study', '.exe')
Similarly in this example, the previous period will be ignored and the ext will be set as .txt including the leading period.
splitext('/Coding Ninjas Studio/study.txt')
('/Coding Ninjas Studio/study', '.txt')
In this example, the path contains only one component having the leading period. Therefore the path is considered as a root and the ext will be empty.
splitext('.codingninjas')
('.codingninjas', '')
Let’s see the implementation of the above examples.
OS Path Splitext() Implementation
import os
# path
path='Coding Ninjas Studio'
root_ext = os.path.splitext(path)
print("root part of '% s' is" % path, root_ext[0])
print("ext part of '% s' is" % path, root_ext[1], "\n")
# path
path='Coding Ninjas Studio.study.exe'
root_ext = os.path.splitext(path)
print("root part of '% s' is" % path, root_ext[0])
print("ext part of '% s' is" % path, root_ext[1], "\n")
#path
path='Coding Ninjas Studio/study.txt'
root_ext = os.path.splitext(path)
print("root part of '% s' is" % path, root_ext[0])
print("ext part of '% s' is" % path, root_ext[1], "\n")
#path
path='.codingninjas'
root_ext = os.path.splitext(path)
print("root part of '% s' is" % path, root_ext[0])
print("ext part of '% s' is" % path, root_ext[1])
You can also try this code with Online Python Compiler
Run Code
Output:
root part of 'Coding Ninjas Studio' is Coding Ninjas Studio
ext part of 'Coding Ninjas Studio' is
root part of 'Coding Ninjas Studio.study.exe' is Coding Ninjas Studio.study
ext part of 'Coding Ninjas Studio.study.exe' is .exe
root part of 'Coding Ninjas Studio/study.txt' is Coding Ninjas Studio/study
ext part of 'Coding Ninjas Studio/study.txt' is .txt
root part of '.codingninjas' is .codingninjas
ext part of '.codingninjas' is
You can practice by yourself with the help of online python compiler.
Now, let’s see some FAQs related to the os path splitext.
Frequently Asked Questions
What does os path Splitext do?
os.path.splitext() is a Python function that splits a pathname into its base name and extension. It returns a tuple containing the base name and extension separately.
What is path split?
The function path split divides a file path into its component file name and directory name and returns each one separately.
What is the use of os path Isfile?
os.path.isfile() is used to check if a given path points to a regular file. It returns True if the path exists and is a file, otherwise False.
What is the return value of os.path.splitext?
The splitext() method returns a tuple representing the root and ext part of the specified pathname.
What is os.path.join used for?
The os.path.join() is a method in Python to join one or more path components.
Must read: Python Square Root
Conclusion
We have discussed os path splitext with its syntax. Also covered the examples and implementation of os path splitext in Python.
After reading about the os path splitext, are you not feeling excited to read/explore more articles? Don't worry; Coding Ninjas has you covered. See Python, Basics of Python, Functions in Python, Convert String to List Python and Testing Framework in Python to learn.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more!
Recommended Problems:
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!