Friday, December 27, 2024
Google search engine
HomeLanguagesPython | os.path.split() method

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




# 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])


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




# 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


Output:

Head of '': 
Tail of '':

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

RELATED ARTICLES

Most Popular

Recent Comments