Change language

Python | os.path.split () method

|

os.path.split (() in Python is used for os.path.split ( ( ) path name to the head and tail pair. Here tail — this is the last component of the path name, and head — whatever leads to this.

For example, consider the following path:

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

B In the above example, the "file.txt" pathname component is tail, and "/ home / User / Desktop /" is the head. The tail will never contain a forward slash; if the path name ends with a forward slash, tail will be empty, and if the path name does not have a forward slash, 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: Using the os.path.split ( ( )

# Python program to explain the os.path.split ( () method

 
# import of the os module

import os

 
# path

path = ’/ home / User / Desktop / file.txt’

 
# Split path into
# steam head and tail

head_tail = os.path.split ( (path)

 
# print head and tail
# specified path

print ( "Head of’% s: ’" % path, head_tail [ 0 ])

print ( " Tail of ’% s:’ " % path, head_tail [ 1 ], "" )

 

 
# path

path = ’/ home / User / Desktop /’

 
# Split path in
# head and tail of steam

head_tail = os.path.split ( (path)

  
# print head and tail
# specified path

print ( " Head of ’% s:’ " % path, head_tail [ 0 ])

print ( "Tail of’% s: ’" % path, head_tail [ 1 ], "" )

 
# path

path = ’file.txt’

 
# Split path into
# steam head and tail

head_tail = os.path.split ( (path)

  
# printhead and tail
# of the specified path

print ( " Head of ’% s:’ " % path, head_tail [ 0 < / code> ])

print ( "Tail of’% s: ’" % path, head_tail [ 1 ])

Exit :

 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 the path is empty

# Python program for an explanation of the os.path.split ( () method

 
# import the os module

import os

  
# path

path = ’’

 
# Split path in
# steam head and tail

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
# returns empty
# head and tail if
# specified path is empty

Exit:

 Head of ’’: Tail of ’’: 

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

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method