Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get device name for a mount in python

Tags:

python

smb

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)
//[email protected]/Projects/SomeProject on /Volumes/MYVOLUME (smbfs, nodev, nosuid, mounted by user)

I would like to get the //[email protected]/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  
like image 911
Omnipresent Avatar asked Mar 27 '26 16:03

Omnipresent


1 Answers

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'
like image 153
PilouPili Avatar answered Mar 29 '26 04:03

PilouPili



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!