3

On my mac I've mapped an SMB share as a Volume. I would like to get the real path of this Volume in my python code.

➜  MYVOLUME pwd
/Volumes/MYVOLUME
➜  MYVOLUME mount
/dev/disk1s1 on / (apfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
/dev/disk1s4 on /private/var/vm (apfs, local, noexec, journaled, noatime, nobrowse)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
//fistname.lastname@10.10.50.20/Projects/SomeProject on /Volumes/MYVOLUME (smbfs, nodev, nosuid, mounted by user)

I would like to get the //fistname.lastname@10.10.50.20/Projects/SomeProject part. I've tried using below but it doesn't get me the actual SMB location I want.

def find_mount_point(self,path):
    path = os.path.abspath(path)
    while not os.path.ismount(path):
        path = os.path.dirname(path)
    return path  
Omnipresent
  • 29,434
  • 47
  • 142
  • 186
  • 1
    That’s not a `path` at all, but a *device* name. – Davis Herring Oct 27 '18 at 21:52
  • The first part of the answer of https://stackoverflow.com/questions/4453602/how-to-find-the-mountpoint-a-file-resides-on plus the comment of @DavisHerring plus https://stackoverflow.com/questions/26112492/iterating-through-mount-points-using-python should give you the answer. – PilouPili Oct 27 '18 at 22:01
  • @NPE Great. That worked! `psutil.disk_partitions(all=True)` did the trick. If you want to replace your comment by an answer, I'd be happy to accept. – Omnipresent Oct 27 '18 at 22:16
  • World you bé offended if I flag this answer as a duplicate then ? – PilouPili Oct 27 '18 at 22:18
  • 1
    Possible duplicate of [Iterating through mount points using Python](https://stackoverflow.com/questions/26112492/iterating-through-mount-points-using-python) – PilouPili Oct 27 '18 at 22:33
  • It seems it is not a duplicate so I posted an answer. – PilouPili Oct 29 '18 at 15:13

1 Answers1

3

You can use psutil library

Working example

def find_sdiskpart(path):
    path = os.path.abspath(path)
    while not os.path.ismount(path):
        path = os.path.dirname(path)
    p = [p for p in psutil.disk_partitions(all=True) if p.mountpoint == path.__str__()]
    l = len(p)
    if len(p) == 1:
        print type(p[0])
        return p[0]
    raise psutil.Error

The function will return a <class 'psutil._common.sdiskpart'>containg mountpoint and device name

Can be used like this

try:
    p = find_sdiskpart(".")
    print p.mountpoint
    print p.device
except psutil.Error:
    print 'strange'
PilouPili
  • 2,601
  • 2
  • 17
  • 31