在Python中,如何获取给定文件路径的文件系统

Uma*_*thy 3 python filesystems filepath

在python中,给定像/ usr/local这样的目录或文件路径,我需要获取其可用的文件系统.在某些系统中,它可能是/(root)本身,而在其他系统中它可能是/ usr.

我试过os.statvfs它没有帮助.我是否必须使用路径名运行df命令并从输出中提取文件系统?有更好的解决方案吗?

它仅适用于linux/unix平台.

谢谢

unu*_*tbu 6

这是这里找到的配方的略微修改版本. os.path.realpath添加了因此符号链接正确处理.

import os
def getmount(path):        
    path = os.path.realpath(os.path.abspath(path))
    while path != os.path.sep:
        if os.path.ismount(path):
            return path
        path = os.path.abspath(os.path.join(path, os.pardir))
    return path
Run Code Online (Sandbox Code Playgroud)