如何从Python中的路径获取挂载的nfs设备

2024-09-28 20:48:51 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从Python中的文件路径找到nfs共享的主机?在

例如,如果df -h给出:

Filesystem              Size  Used Avail Use% Mounted on
/dev/sdb3               500G  200G  300G  40% /
hostname:/local           1T   20G  980G   2% /mnt/drive

当给定/mnt/drive/file.txt时,如何得到“hostname”?在


Tags: 文件dev路径dfsizeondrivehostname
1条回答
网友
1楼 · 发布于 2024-09-28 20:48:51

挂载点可以从mount -l的输出解析:

#!/usr/bin/env python

import subprocess

mounts = {}    
for line in subprocess.check_output(['mount', '-l']).split('\n'):
    parts = line.split(' ')
    if len(parts) > 2:
        mounts[parts[2]] = parts[0]

print(mounts)

然后可以将路径与mounts字典中的键进行比较,并进一步解析这些值以获得挂载点。在

来源:https://askubuntu.com/a/189990/124703

相关问题 更多 >