Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Get Mount Point on Windows or Linux

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.

like image 415
noahd Avatar asked Jul 16 '09 15:07

noahd


People also ask

How do I find my mount point in Python?

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.

How do I find my mount point in Linux?

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=/

Is mount point Linux?

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.

How do you check if a device is mounted on Linux?

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.


2 Answers

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.

like image 56
Alex Martelli Avatar answered Sep 22 '22 03:09

Alex Martelli


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.')
like image 32
Mark Avatar answered Sep 26 '22 03:09

Mark