Python

Python OS Path Split

Python provides various modules and methods for working with the files and their paths. For instance, extracting the directory name from a path, getting the file name from a path, or simply splitting a path into two parts. In such situations, the “os.path.split()” method is used in Python. This method takes the path and retrieves a tuple containing the head and tail of the path.

This write-up presents a comprehensive guide on Python’s “os.path.split()” method using numerous examples and covering the below content:

What is the “os.path.split()” Method in Python?

In Python, the “os.path.split()” method is utilized to split/separate a given path name into groups of “head” and “tail”. The head is the directory name up to the last path separator, and the “tail” is the file name or an empty string if there is no path separator.

Syntax

os.path.split(path)

 
Parameters

In the above syntax, the “path” parameter is an “str/string” or “bytes” path-like object corresponding to a path on the file system.

Return Value

The “os.path.split()” method returns a tuple of two strings, the first string being the “head” and the second string being the “tail”. If the path does not contain any path separators, then the head will be empty, and the tail will be the same as the path.

Example 1: Applying the “os.path.split()” Method to Split the Specified Path

The below code is implemented to split the path into head and tail pair:

import os
path = r'C:\Users\p\Documents\program\filename.txt'
print(os.path.split(path), '\n')

path =r'C:\Users\p\Documents\program'
print(os.path.split(path), '\n')

path = 'filename.txt'
print(os.path.split(path), '\n')

 
In the above code:

    • The “os” module is imported.
    • The “os.path.split()” method takes the complete path, including the file name retrieving the tuple with the head and tail of the path.
    • The “os.path.split()” method is used again twice with the specified path without the filename and with the filename, respectively to retrieve the head and tail in the form of a tuple.

Output


The head and tail pair of the specified paths have been returned by the “os.path.split()” method, accordingly.

Example 2: Applying the “os.path.split()” Method to Split the Empty Path

This code is used to split the empty path:

import os
path = ''
print(os.path.split(path))

 
In the above code, the “os.path.split()” method takes an “empty path” as an argument and retrieves a tuple containing the head and tail of the path.

Output


The empty path is split into an empty head and tail.

Conclusion

The “os.path.split()” method of the “os” module is used to split/separate a specified path and return a tuple containing the “head” and “tail” of the path. This method takes the empty path and returns an empty tuple without any head and tail path value. This post presented a detailed tutorial of Python’s “os.path.split()” method using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.