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.
Code #1: Use of os.path.splitdrive() method (On Windows)
import os
path = R "C:\User\Documents\file.txt"
drive_tail = os.path.splitdrive(path)
print ( "Drive of path '%s:'" % path, drive_tail[ 0 ])
print ( "Tail of path '%s:'" % path, drive_tail[ 1 ], "\n" )
path = R "\\host\computer\dir\file.txt"
drive_tail = os.path.splitdrive(path)
print ( "Drive of path '%s':" % path, drive_tail[ 0 ])
print ( "Tail of path '%s':" % path, drive_tail[ 1 ], "\n" )
path = R "\dir\file.txt"
drive_tail = os.path.splitdrive(path)
print ( "Drive of path '%s':" % path, drive_tail[ 0 ])
print ( "Tail of path '%s':" % path, drive_tail[ 1 ])
|
Output:
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)
import os
path = "/home/User/Documents/file.txt"
drive_tail = os.path.splitdrive(path)
print ( "Drive of path '%s':" % path, drive_tail[ 0 ])
print ( "Tail of path '%s':" % path, drive_tail[ 1 ])
|
Output:
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Jun, 2019
Like Article
Save Article