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
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'
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