AppDividend
Latest Code Tutorials

Python os.path.splitext() Function: The Complete Guide

0

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.

Python os.path.splitext()

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.

Syntax

os.path.splitext(path)

Arguments

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.

Return Value

The splitext() method returns a tuple that represents the root and ext part of the specified pathname.

Example

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[0])
print("The ext part is: ", root_extension[1])

Output

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)

Output

/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:])

Output

/Users/krunal/Desktop/code/pyt/app
pyt

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)

Output

/Users/krunal/Desktop/code/pyt/app
.pyt
/Users/krunal/Desktop/code/pyt/app.sql

That’s it for this tutorial.

See also

Python os.path.join()

Python os.path.exists()

Python os.path.abspath()

Python os.path.getsize()

Python os.path.isfile()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.