當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python os.path.splitdrive()用法及代碼示例


Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。 os.path模塊是Python中OS模塊的子模塊,用於通用路徑名操作。

os.path.splitdrive()Python中的方法用於將路徑名稱拆分為一對驅動器和尾部。在這裏,驅動器是安裝點或空字符串,其餘路徑組件是尾部。

在不使用驅動器規範的係統上,驅動器將始終為空字符串。示例:UNIX。


在Windows上,os.path.splitdrive()方法將給定的路徑名​​分為驅動器或UNC共享點作為驅動器,將其他路徑組件作為尾部。例如:

     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   
用法: os.path.splitdrive(path)

參數:
path:代表文件係統路徑的path-like對象。 path-like對象是表示路徑的str或bytes對象。

返回類型:此方法返回一個表示給定路徑名的驅動器和尾部的元組。

代碼1:os.path.splitdrive()方法的使用(在Windows上)
# 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 

代碼2:用於os.path.splitdrive()方法(在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

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



相關用法


注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.path.splitdrive() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。