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 a submodule of OS module in Python used for common pathname manipulation.
os.path.splitdrive()
method in Python is used to Split the path name into a pair drive and tail. Here, drive is either a mount point or the empty string and rest path component is tail.
On systems which do not use drive specification, drive will always be an empty string. Example: UNIX.
On Windows, os.path.splitdrive()
method splits a given path name into drive or UNC sharepoint as drive and other path component as tail.
For example:
path name drive tail On Windows If path contains drive letter C:\User\Documents\file.txt C: C:\User\Documents\file.txt If the path contains UNC path \\host\computer\dir\file.txt \\host\computer \dir\file.txt On Unix /home/User/Documents/file.txt {empty} /home/User/Documents/file.txt
Syntax: os.path.splitdrive(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 drive and tail of the given path name.
# Python program to explain os.path.splitdrive() method # importing os module import os # Path Containing a drive letter path = R "C:\User\Documents\file.txt" # Split the path in # drive and tail pair drive_tail = os.path.splitdrive(path) # print drive and tail # of the given path print ( "Drive of path '%s:'" % path, drive_tail[ 0 ]) print ( "Tail of path '%s:'" % path, drive_tail[ 1 ], "\n" ) # Path representing a UNC path path = R "\\host\computer\dir\file.txt" # Split the path in # drive and tail pair drive_tail = os.path.splitdrive(path) # print drive and tail # of the given path print ( "Drive of path '%s':" % path, drive_tail[ 0 ]) print ( "Tail of path '%s':" % path, drive_tail[ 1 ], "\n" ) # Path representing a relative path path = R "\dir\file.txt" # Split the path in # drive and tail pair drive_tail = os.path.splitdrive(path) # print drive and tail # of the given path print ( "Drive of path '%s':" % path, drive_tail[ 0 ]) print ( "Tail of path '%s':" % path, drive_tail[ 1 ]) |
Drive of path 'C:\User\Documents\file.txt': C: Tail of path 'C:\User\Documents\file.txt': \User\Documents\file.txt Drive of path '\\host\computer\dir\file.txt': \\host\computer Tail of path '\\host\computer\dir\file.txt': \dir\file.txt Drive of path '\dir\file.txt': Tail of path '\dir\file.txt': \dir\file.txt
Code #2: Use of os.path.splitdrive()
method (On UNIX)
# Python program to explain os.path.splitdrive() method # importing os module import os # Path path = "/home/User/Documents/file.txt" # Split the path in # drive and tail pair drive_tail = os.path.splitdrive(path) # print drive and tail # of the given path print ( "Drive of path '%s':" % path, drive_tail[ 0 ]) print ( "Tail of path '%s':" % path, drive_tail[ 1 ]) # os.path.splitdrive() method # will return drive as empty everytime # as UNIX do not use # drive specification |
Drive of path '/home/User/Documents/file.txt': Tail of path '/home/User/Documents/file.txt': /home/User/Documents/file.txt
Reference: https://docs.python.org/3/library/os.path.html
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.