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.
ismount() method in Python is used to check whether the given path is a mount point or not. A mount point is a point in a file system where different file system has been mounted.
To display only the mount point where the filesystem with label "/boot" or “/” is mounted, use the following command. # findmnt -n --raw --evaluate --output=target LABEL=/boot OR # findmnt -n --raw --evaluate --output=target LABEL=/
A mount point is simply a directory, like any other, that is created as part of the root filesystem. So, for example, the home filesystem is mounted on the directory /home. Filesystems can be mounted at mount points on other non-root filesystems but this is less common.
The findmnt command is able to search in /etc/fstab , /etc/fstab. d , /etc/mtab or /proc/self/mountinfo . If device or mountpoint is not given, all filesystems are shown. The command prints all mounted filesystems in the tree-like format by default.
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.
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.')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With