2024-10-02 10:29:49 发布
网友
如果有一个预定义的简单易记的python命令,从mac和windows上都有用的字符串文件路径中提取驱动器号?在
如果MAC:
filepathString = '/Volumes/Some Documents/The Doc.txt'
会导致:
如果赢了:
filepathString = 'c:\Some Documents\The Doc.txt'
myDrive = 'c:'
简单示例(Windows):
import os import pathlib drive = pathlib.Path(os.getcwd()).parts[0]
我想你必须为此编写自定义代码。使用platform.system()或platform.uname()找出您所在的系统类型,然后使用os.path函数以适合检测到的平台的方式提取驱动器/卷名。在
platform.system()
platform.uname()
os.path
草图:
def volume_name(path): if platform.system() == "Darwin": return re.search("^\/Volumes\/[^/]+/", path).group(0) elif platform.system() == "Windows": return path[0:2]
尝试来自^{}模块的^{}方法和常规的^{}。它不是单行的,但是我还是看不出代码是如何知道将transfer追加到卷路径的,或者检测给定路径是否来自Windows或Unix(记住C:是一个有效的Unix文件名)。在
transfer
C:
简单示例(Windows):
我想你必须为此编写自定义代码。使用
platform.system()
或platform.uname()
找出您所在的系统类型,然后使用os.path
函数以适合检测到的平台的方式提取驱动器/卷名。在草图:
尝试来自^{} 模块的^{} 方法和常规的^{} 。它不是单行的,但是我还是看不出代码是如何知道将
transfer
追加到卷路径的,或者检测给定路径是否来自Windows或Unix(记住C:
是一个有效的Unix文件名)。在相关问题 更多 >
编程相关推荐