Python | os.path.split() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common pathname manipulation.

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

For example consider the following path name:



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

In the above example ‘file.txt’ component of path name is tail and ‘/home/User/Desktop/’ is head.The tail part will never contain a slash; if name of the path ends with a slash, tail will be empty and if there is no slash in path name, 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'

Syntax: os.path.split(path)

Parameter:
path: 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.

Code #1: Use of os.path.split() method

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Code #2: If path is empty

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.path.split() method 
    
# importing os module 
import os
  
# path
path = ''
  
# Split the path in 
# head and tail pair
head_tail = os.path.split(path)
  
# print head and tail
# of the specified path
print("Head of '% s':" % path, head_tail[0])
print("Tail of '% s':" % path, head_tail[1])
  
  
# os.path.split() function
# will return empty
# head and tail if 
# specified path is empty

chevron_right


Output:

Head of '': 
Tail of '':

Reference: https://docs.python.org/3/library/os.path.html



My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.