首页 > 解决方案 > 使用 python 查找 nfs 服务器的名称

问题描述

我希望找到一种方法来查找来自 python 中的特定文件/目录的 nfs 服务器的名称,而不是使用外部命令。

这是我到目前为止所拥有的,看起来太脆弱了。首先,shell命令本身:

$ findmnt -Do SOURCE /scratch
SOURCE
scratch_server.example.com:/scratch

蟒蛇内部:

import subprocess, sys
cmd = ['findmnt', '-Do', 'SOURCE', '/scratch']
proc = subprocess.run(cmd, capture_output=True)
server = proc.stdout.decode(sys.stdout.encoding).split('\n')[1].split(':')[0]
print(server)
scratch_server.example.com

有没有更好、更“pythonic”的方式来做到这一点?我无法想象这是最佳实践。需要明确的是,我根本不关心执行子进程、超时等的最佳方式。我真的完全不必在子流程中执行此操作。谢谢。

标签: pythonmountnfslinux-disk-free

解决方案


推荐阅读