Open In App

Python | os.path.split() method

Improve
Improve
Like Article
Like
Save
Share
Report

os.path.split() method in Python is used to Split the path name into a pair, and tail. Here, the tail is the last path name component and the head is everything leading up to that.

For example, consider the following path names:

path name = '/home/User/Desktop/file.txt'

In the above example, the ‘file.txt’ component of the path name is tail, and â€˜/home/User/Desktop/’ is the head. The tail part will never contain a slash; if the name of the path ends with a slash, the tail will be empty and if there is no slash in the path name, the head will be empty.

For example:

     path                             head                 tail
'/home/user/Desktop/file.txt'   '/home/user/Desktop/'   'file.txt'
'/home/user/Desktop/'           '/home/user/Desktop/'    {empty}
'file.txt'                           {empty}            'file.txt'

os.path.split() Method Syntax in Python

Syntax: os.path.split(path)

Parameter: A path-like object representing a file system path. A path-like object is either a str or bytes object representing a path.

Return Type: This method returns a tuple that represents head and tail of the specified path name.

Python os.path.split() Method Examples

Below are some examples of os.path.split() function of OS Path module in Python:

Split Path Name Using os.path.split() Function in Python

In this example, the code demonstrates the use of os.path.split() method to split a given file path into its directory (head) and file/directory name (tail). It prints the head and tail for three different path examples: ‘/home/User/Desktop/file.txt‘, ‘/home/User/Desktop/‘, and ‘file.txt‘.

Python3




# importing os module
import os
 
# path
path = '/home/User/Desktop/file.txt'
 
# Split the path
head_tail = os.path.split(path)
 
# print head and tail
print("Head of '% s:'" % path, head_tail[0])
print("Tail of '% s:'" % path, head_tail[1], "\n")
 
path = '/home/User/Desktop/'
 
# Split the path
head_tail = os.path.split(path)
 
# print head and tail
print("Head of '% s:'" % path, head_tail[0])
print("Tail of '% s:'" % path, head_tail[1], "\n")
 
path = 'file.txt'
 
# Split the path
head_tail = os.path.split(path)
 
# print head and tail
print("Head of '% s:'" % path, head_tail[0])
print("Tail of '% s:'" % path, head_tail[1])


Output:

Head of '/home/User/Desktop/file.txt': /home/User/Desktop
Tail of '/home/User/Desktop/file.txt': file.txt 

Head of '/home/User/Desktop/': /home/User/Desktop
Tail of '/home/User/Desktop/':  

Head of 'file.txt': 
Tail of 'file.txt': file.txt

Split Path Name If Path is Empty Using os.path.split()

In this example, this code demonstrates the use of the os.path.split() method with an empty path. It shows that when an empty path is provided, the os.path.split() function returns empty strings for both the head and tail, as indicated in the comments.

Python3




# importing os module
import os
 
path = ''
 
# Split the path
head_tail = os.path.split(path)
 
# print head and tail
print("Head of '% s':" % path, head_tail[0])
print("Tail of '% s':" % path, head_tail[1])


Output:

Head of '': 
Tail of '':


Last Updated : 12 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads