Jump to content

Python OS Path Split

Linux Hint

Recommended Posts

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.




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.


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 = ''

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.


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


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.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...