如何在python中获取安装的设备名称

时间:2018-10-27 21:47:49

标签: python smb

在我的Mac上,我已将SMB共享映射为卷。我想在我的python代码中获得此卷的真实路径。

➜  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)

我想获得//fistname.lastname@10.10.50.20/Projects/SomeProject部分。我尝试在下面使用,但没有得到我想要的实际SMB位置。

def find_mount_point(self,path):
    path = os.path.abspath(path)
    while not os.path.ismount(path):
        path = os.path.dirname(path)
    return path  

1 个答案:

答案 0 :(得分:0)

您可以使用psutil

工作示例

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

该函数将返回包含<class 'psutil._common.sdiskpart'>的安装点和设备名称

可以这样使用

try:
    p = find_sdiskpart(".")
    print p.mountpoint
    print p.device
except psutil.Error:
    print 'strange'