当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。