Python: Get Mount Point on Windows or Linux
Solution 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).
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.
Solution 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.
See here for details: http://www.modzone.dk/forums/showthread.php?threadid=278
I would try 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.')
Evan.zz
Updated on June 21, 2022Comments
-
Evan.zz almost 2 years
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:
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.
-
Jason Baker almost 15 yearsIt is possible to have a volume mounted within another drive as well (although I'm not sure if that's the correct terminology).
-
rogerdpack almost 14 yearsNB that the win32api is from a package installed separately sourceforge.net/projects/pywin32/files Thanks for your answer--it also led me to discover how to get the DVD disk tite