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 ( ( )
|
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
|
Exit:
Head of ’’: Tail of ’’: