Python | os.path.splitdrive() method

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)

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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])

chevron_right


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)

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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

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.




My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.