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上安装网络,所以我也希望能够检测到该安装。
3 个解决方案
#1
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:,或者\\ hostname(带有两个前导反斜杠:仔细转义或在Python fpr中使用r'...'表示法这样的文字字符串)。
edit: since NTFS 5.0 mount points are supported, but according to this post the API for them is in quite a state -- "broken and ill-documented", the post's title says. Maybe executing the microsoft-supplied mountvol.exe is 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驱动:path / L应该发出指定路径的挂载卷名,或者只是mountvol这样列出所有这样的挂载(我不得不说“应该”因为我现在不能检查)。您可以使用subprocess.Popen执行它并检查其输出。
#2
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.')
#3
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)
#1
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:,或者\\ hostname(带有两个前导反斜杠:仔细转义或在Python fpr中使用r'...'表示法这样的文字字符串)。
edit: since NTFS 5.0 mount points are supported, but according to this post the API for them is in quite a state -- "broken and ill-documented", the post's title says. Maybe executing the microsoft-supplied mountvol.exe is 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驱动:path / L应该发出指定路径的挂载卷名,或者只是mountvol这样列出所有这样的挂载(我不得不说“应该”因为我现在不能检查)。您可以使用subprocess.Popen执行它并检查其输出。
#2
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.')
#3
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)