Python os.path.splitdrive() Method

Python os.path.splitdrive() Method

  1. Syntax of Python os.path.splitdrive() Method
  2. Example 1: Use the os.path.splitdrive() Method in Python
  3. Example 2: Use the os.path.splitdrive() Method on Windows OS
  4. Example 3: Use the os.path.splitdrive() Method on UNIX
  5. Example 4: Create a Different File Extension Using the os.path.splitdrive() Method on Window OS

Python os.path.splitdrive() method is an efficient way to split a path name into a pair drive and tail. A drive is the share point or an empty string; the rest of the path component is the tail.

Different OS might have a drive or not, based on their specification.

Syntax of Python os.path.splitdrive() Method

os.path.splitdrive(path)

Parameters

path an address object of a file system path or a symlink. The object can either be an str or bytes

Return

The return type of this method is a tuple representing the drive and tail of the specified path name.

Example 1: Use the os.path.splitdrive() Method in Python

import os

path = "home/user/File.txt"

split = os.path.splitdrive(path)

print("The drive of path '%s': " %path, split[0])

print("The tail of path '%s': " %path, split[1])

Output:

The drive of path 'home/user/File.txt':
The tail of path 'home/user/File.txt':  home/user/File.txt

As seen in the above code, the first element of the tuple is the drive, and the subsequent one is the tail component. In any case, concatenating the drive and tail components will always make the original path.

Example 2: Use the os.path.splitdrive() Method on Windows OS

import os

path = "C:/home/user/File.txt"

split = os.path.splitdrive(path)

print("The drive of path '%s': " %path, split[0])

print("The tail of path '%s': " %path, split[1])

Output:

The drive of path 'home/user/File.txt':  C:
The tail of path 'home/user/File.txt':  home/user/File.txt

Note that the Windows OS uses drive specification, so the drive component of the tuple is never empty.

Example 3: Use the os.path.splitdrive() Method on UNIX

import os

path = "C:/home/user/File.txt"

split = os.path.splitdrive(path)

print("The drive of path '%s': " %path, split[0])

print("The tail of path '%s': " %path, split[1])

Output:

The drive of path 'C:/home/user/File.txt':
The tail of path 'C:/home/user/File.txt':  C:/home/user/File.txt

Note that the UNIX OS does not use drive specifications, so the drive component of the tuple is always empty.

Example 4: Create a Different File Extension Using the os.path.splitdrive() Method on Window OS

import os

path = "C:/home/user/File.txt"

split = os.path.splitdrive(path)

print("The root part is: ",split[0])

print("The ext part is: ",split[1])

path_address = split[0] + '/Desktop/File.txt'

print("The full path address is: ", path_address)

Output:

The root part is:  C:
The ext part is:  /home/user/File.txt
The full path address is:  C:/Desktop/File.txt

Using the above code, you can easily have a new file address with a new name.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method