Python os.path.splitext() Function: The Complete Guide
The os.path.split() is a built-in Python function that splits a path into two parts: everything before the final slash and everything after it. The OS module provides methods and properties for interacting with the operating system. It comes under Python’s standard utility modules. Let’s see how to use the os.path.splitext() method and its various use cases.
The os.path.splitext() is a built-in Python function that splits the pathname into the pair of root and ext. The ext stands for extension and has the extension portion of the specified path, while the root is everything except the ext part.
To extract the extension of a file name in Python, use the os.path.splitext() method.
If the ext is empty, that means the specified path does not have any extension. If the specified path has a leading period (‘.’), it will be ignored.
It is a path-like object representing a file system path. The system path-like object is either a string or bytes object representing a path.
The splitext() method returns a tuple that represents the root and ext part of the specified pathname.
Let’s define a full path and pass that path to the os.path.splitext() method.
import os path = "/Users/krunal/Desktop/code/pyt/app.pyt" root_extension = os.path.splitext(path) print("The output tuple", root_extension) print("The root part is: ", root_extension) print("The ext part is: ", root_extension)
The output tuple ('/Users/krunal/Desktop/code/pyt/app', '.pyt') The root part is: /Users/krunal/Desktop/code/pyt/app The ext part is: .pyt
You can see that the first output is a complete tuple containing the root and extension of the file path.
The second output is only the root part of the full path.
The third output is only the extension part of the full path.
The os.path.splitext() function splits at the last (right) dot. If you want to split by the first (left) dot ., use the os.path.split() function.
To extract a directory name from the file path, use the os.path.dirname() function.
Concatenating with the + operator returns the original path string.
import os path = "/Users/krunal/Desktop/code/pyt/app.pyt" root, extension = os.path.splitext(path) print(root) print(extension) full_path = root + extension print(full_path)
/Users/krunal/Desktop/code/pyt/app .pyt /Users/krunal/Desktop/code/pyt/app.pyt
Get the extension without dot (period)
To get the extension from a file path without dot or period in Python, slice the ext part of the splitext() output.
import os path = "/Users/krunal/Desktop/code/pyt/app.pyt" root, extension = os.path.splitext(path) print(root) print(extension[1:])
Create a path string with a different extension
To create a file string with only the extension changed from the original file string, you need first to concatenate the root of the tuple returned by os.path.splitext() with any extension, and you will have a new file path with a new file name.
import os path = "/Users/krunal/Desktop/code/pyt/app.pyt" root, extension = os.path.splitext(path) print(root) print(extension) new_full_path = root + '.sql' print(new_full_path)
/Users/krunal/Desktop/code/pyt/app .pyt /Users/krunal/Desktop/code/pyt/app.sql
That’s it for this tutorial.