Change language

Python | os.path.splitdrive () method

|

os.path.split (drive() in Python is used to split a path name into a pair drive and tail . Here disk — it is either a mount point or an empty string and the rest of the path — this is the tail .

On systems that do not use the disk specification, disk will always be a blank line. Example: UNIX.

On Windows, the os.path.split (drive() method splits the given path name into disk or os.path.split (drive () UNC as drive, and another path component — like tail
For example :

  path name   drive   tail   On Windows  If the path contains drive letter C: UserDocumentsfile.txt C: C: UserDocumentsfile.txt If the path contains UNC path  hostcomputerdirfile.txt  hostcomputer dirfile.txt  On Unix  / home / User / Documents / file.txt {empty} /home/User/Documents/file.txt 

Syntax: os.path.split (drive ( 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: Using the os.path.split (drive ( ) (on Windows)

# Python program to explain the os.path.split (drive () method

 
# import of the os module

import os

 
# Path Contains the drive letter

path = R "C: UserDocumentsfile.txt"

 
  # Split path in
# disk and tail pair

drive_tail = os.path.split (drive (path)

 
# Printer and tail
No given path

print ( "Drive of path’% s: ’" % path, drive_tail [ 0 ])

print ( "Tail of path’% s: ’" % path, drive_tail [ 1 ], " " )

  # Path representing UNC path

path = R " hostcomputerdirfile.txt"

 
# Split path in
# drive and tail pair

drive_tail = os.path.split (drive (path)

 
# Printer and tail
No. of this path

print ( "Drive of path’% s’: " % path, drive_tail [ 0 ])

print ( " Tail of path ’% s’:" % path, drive_tail [ 1 ], "" )

 
# Path representing a relative path

path = R "dirfile.txt "

  
# Split path in
# drive and tail pair

drive_tail = os.path.split (drive (path)

 
# Printer and tail
No of this path

print (  "Drive of path’% s’: " % path, drive_tail [ 0 ])

print ( "Tail of path’% s’: " % path, drive_tail [ 1 ])

Exit :

 Drive of path ’C: UserDocumentsfile.txt’: C: Tail of path’ C : UserDocumentsfile.txt’: UserDocumentsfile.txt Drive of path ’ hostcomputerdirfile.txt’:  hostcomputer Tail of path’  hostcomputerdirfile.txt’: dirfile.txt Drive of path ’dirfile.txt’: Tail of path’ dirfile.txt ’: dirfile.txt 

Code # 2: Using os.path.split (drive () (UNIX)

# Python program to explain the os.path.split (drive () method

 
# import of the os module

import os

 
# Path

path = "/ home / User / Documents / file.txt"

 
# Split path into
# drive and tail pair

drive_tail = os.path.split (drive ( path)

 
# Printer and tail
No of this path

print ( "Drive of path’% s’: " % path, drive_tail [ 0 ])

print ( "Tail of path’% s’: " % path, drive_tail [ 1 ])

  

  
# os.path.split (drive () method
# will return the disk as empty every time
# how not to use UNIX
# disk spec

Output:

 Drive of path ’/ home / User / Documents / file.txt’: Tail of path’ / home / User / Documents / file.txt’: / home / User / Documents / file. txt 

Link: https://docs.python.org/3/ library / os.path.html

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically