Python os.path.split() Method

Python os.path.split() Method

  1. Syntax of the Python os.path.split() Method
  2. Example Codes: Split Path With the os.path.split() Method
  3. Example Codes: Split Path Into Steam Head and Tail in the os.path.split() Method
  4. Example Codes: Set Path Address as Empty in the os.path.split() Method
  5. Example Codes: Unpack the Tuples in the os.path.split() Method

Python os.path.split() method splits a path name to return a tuple head and tail. The folder and directory address can be easily retrieved using this method.

Syntax of the Python os.path.split() Method

os.path.split(path address)

Parameters

path address It is an address object of a file system path. The Object can either be an str or bytes.

Return

This method returns a pair of head and tail of the required directory path.

Example Codes: Split Path With the os.path.split() Method

import os

directory = '/home/user/Desktop/fileName.txt'

head_tail = os.path.split(directory)

print(head_tail)

Output:

('/home/user/Desktop', 'fileName.txt')

os.path.split() is a conglomerate of two separate Python methods os.path.basename(), for returning a file name, and os.path.dirname(), for returning the directory path. The part of the address before the final slash becomes the head, and the end component is the tail.

Example Codes: Split Path Into Steam Head and Tail in the os.path.split() Method

import os

directory = '/home/user/Desktop/fileName.txt'

head_tail = os.path.split(directory)

print("Head of '",directory,":' {1}".format(head_tail, head_tail[0]))
print("Tail of '",directory,":' {1}".format(head_tail, head_tail[1]), "\n")

Output:

Head of ' /home/user/Desktop/fileName.txt :' /home/user/Desktop
Tail of ' /home/user/Desktop/fileName.txt :' fileName.txt

os.path.split() returns an array, and respective head and tail can be retrieved element-wise. The first element of the array is the head component, and the tail is stored as the second element.

Example Codes: Set Path Address as Empty in the os.path.split() Method

import os

address = ""

head_tail = os.path.split(address)

print(head_tail)

Output:

('', '')

os.path.split() returns empty strings when an empty path is entered as its parameter.

Example Codes: Unpack the Tuples in the os.path.split() Method

import os

filePath = "/home/user/Desktop/fileName.txt"

directoryName, fileName = os.path.split(filePath)

print(directoryName)
print(fileName)

Output:

/home/user/Desktop
fileName.txt

The return values of os.path.split() can be stored in two variables, for head and tail, respectively.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method