Python:在 Windows 或 Linux 上获取挂载点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1138383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Get Mount Point on Windows or Linux
提问by noahd
I need a function to determine if a directory is a mount point for a drive. I found this code already which works well for linux:
我需要一个函数来确定目录是否是驱动器的安装点。我发现这个代码已经适用于 linux:
def getmount(path):
path = os.path.abspath(path)
while path != os.path.sep:
if os.path.ismount(path):
return path
path = os.path.abspath(os.path.join(path, os.pardir))
return path
But I'm not sure how I would get this to work on windows. Can I just assume the mount point is the drive letter (e.g. C:)? I believe it is possible to have a network mount on windows so I'd like to be able to detect that mount as well.
但我不确定如何让它在 Windows 上工作。我可以假设挂载点是驱动器号(例如 C:)吗?我相信可以在 Windows 上进行网络安装,因此我也希望能够检测到该安装。
采纳答案by Alex Martelli
Windows didn't use to call them "mount points" [edit: it now does, see below!], and the two typical/traditional syntaxes you can find for them are either a drive letter, e.g. Z:
, or else \\hostname
(with two leading backslashes: escape carefully or use r'...'
notation in Python fpr such literal strings).
Windows 不习惯称它们为“挂载点”[编辑:现在可以,见下文!],您可以为它们找到的两种典型/传统语法要么是驱动器号,例如Z:
,要么是 else \\hostname
(带有两个前导反斜杠:小心转义或r'...'
在 Python fpr 这样的文字字符串中使用符号)。
edit: since NTFS 5.0 mount points are supported, but according to this postthe API for them is in quite a state -- "broken and ill-documented", the post's title says. Maybe executing the microsoft-supplied mountvol.exeis the least painful way -- mountvol drive:path /L
should emit the mounted volume name for the specified path, or just mountvol
such list all such mounts (I have to say "should" because I can't check right now). You can execute it with subprocess.Popen
and check its output.
编辑:由于支持 NTFS 5.0 挂载点,但根据这篇文章,它们的 API 处于相当的状态——“损坏且文档不全”,帖子的标题说。也许执行微软提供的mountvol.exe是最不痛苦的方式——mountvol drive:path /L
应该发出指定路径的挂载卷名,或者只是mountvol
列出所有这些挂载(我不得不说“应该”,因为我现在无法检查)。您可以执行它subprocess.Popen
并检查其输出。
回答by Mark
Do you want to find the mount point or just determine if it is a mountpoint?
您是要查找挂载点还是只是确定它是否是挂载点?
Regardless, as commented above, it is possible in WinXP to map a logical drive to a folder.
无论如何,如上所述,在 WinXP 中可以将逻辑驱动器映射到文件夹。
See here for details: http://www.modzone.dk/forums/showthread.php?threadid=278
详情请看这里:http: //www.modzone.dk/forums/showthread.php?threadid=278
I would try win32api.GetVolumeInformation
我会尝试 win32api.GetVolumeInformation
>>> import win32api
>>> win32api.GetVolumeInformation("C:\")
('LABEL', 1280075370, 255, 459007, 'NTFS')
>>> win32api.GetVolumeInformation("D:\")
('CD LABEL', 2137801086, 110, 524293, 'CDFS')
>>> win32api.GetVolumeInformation("C:\TEST\") # same as D:
('CD LABEL', 2137801086, 110, 524293, 'CDFS')
>>> win32api.GetVolumeInformation("\\servername\share\")
('LABEL', -994499922, 255, 11, 'NTFS')
>>> win32api.GetVolumeInformation("C:\WINDOWS\") # not a mount point
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pywintypes.error: (144, 'GetVolumeInformation', 'The directory is not a subdirectory of the root directory.')
回答by Kevin Buchs
Here is some code to return the UNC path pointed to by a drive letter. I suppose there is a more slick way to do this, but I thought I'd contribute my small part.
这是一些返回驱动器号指向的 UNC 路径的代码。我想有一种更巧妙的方法可以做到这一点,但我想我会贡献我的一小部分。
import sys,os,string,re,win32file
for ch in string.uppercase: # use all uppercase letters, one at a time
dl = ch + ":"
try:
flds = win32file.QueryDosDevice(dl).split("\x00")
except:
continue
if re.search('^\\Device\\LanmanRedirector\\',flds[0]):
flds2 = flds[0].split(":")
st = flds2[1]
n = st.find("\")
path = st[n:]
print(path)