Python os.path.splitext() method Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report os.path.splitext() method is used to split the pathname into a pair (root, ext), where root is the part of the path before the file extension and ext is the file extension itself. It is particularly useful when you need to extract the file extension or handle files dynamically based on their type. os.path module is submodule of OS module in Python used for common pathname manipulation.For example consider the following path names: Path Name RootExtension/home/User/Desktop/file.txt/home/User/Desktop/file.txt/home/User/Desktop/home/User/Desktop(empty)file.pyfile.py.txt(empty).txtExample:Let's see a basic example of how os.path.splitext() works: Python import os file_path = "example_file.txt" # Splitting the file path root, ext = os.path.splitext(file_path) print(f"Root: {root}") print(f"Extension: {ext}") OutputRoot: example_file Extension: .txt Explanation:The file_path is "example_file.txt".The function splits it into root ("example_file") and ext (".txt").Syntaxos.path.splitext(path)Parameterspath: The complete file path, which can be relative or absolute.Return ValueThe method returns a tuple with two elements:root: The part of the path before the extension (file name without the extension).ext: The file extension, including the dot (e.g., .txt, .jpg, .py).Example of os.path.splitext() method1. Using os.path.splitext() to Split Path into Root and Extension Python 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]) Outputroot 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/': Explanation:The example demonstrates how to use os.path.splitext() to split a file path into its root and extension parts.The first part of the code shows a path with a file extension, and the second part shows a path without a file extension (just a directory).2. Path Without an ExtensionIf the file doesn't have an extension, the function will return an empty string for the extension. Python import os file_path = "example_file" # Splitting the file path root, ext = os.path.splitext(file_path) print(f"Root: {root}") print(f"Extension: {ext}") OutputRoot: example_file Extension: Explanation: Since "example_file" doesn't have an extension, ext will be an empty string. Comment More infoAdvertise with us Next Article Python | os.path.join() method I ihritik Follow Improve Article Tags : Python python-os-module Python OS-path-module Practice Tags : python Similar Reads os.path.isdir() method - Python os.path.isdir() method in Python checks whether the specified path is a directory or not. This method follows the symbolic link, which means if the specified path is a symbolic link pointing to an existing directory then the method will return True.Example: Using os.path.isdir() methodThis code demo 2 min read Python | os.path.islink() method 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 sub module of OS module in Python used for common path name man 2 min read Python | os.path.ismount() method os.path module is sub module of Python OS Module in Python used for common path name manipulation. os.path.ismount() method in Python is used to check whether the given path is a mount point or not. A mount point is a point in a file system where a different file system has been mounted. os.path.ism 2 min read Python | os.path.join() method The os.path.join() method is a function in the os module that joins one or more path components intelligently. It constructs a full path by concatenating various components while automatically inserting the appropriate path separator (/ for Unix-based systems and \ for Windows). ExamplePythonimport 4 min read Python | os.path.normcase() method 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 sub module of OS module in Python used for common path name man 2 min read Python | os.path.normpath() method 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 sub module of OS module in Python used for common path name man 2 min read Python | os.path.realpath() method 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 sub module of OS module in Python used for common path name man 2 min read Python | os.path.relpath() method 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 sub module of OS module in Python used for common path name man 3 min read Python | os.path.samefile() method 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 sub module of OS module in Python used for common path name man 2 min read Python | os.path.sameopenfile() method 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 sub module of OS module in Python used for common path name man 3 min read Like