您可以调用
mount命令并解析其输出以找到路径中最长的公用前缀,或者使用
stat系统调用获取文件所在的设备,然后沿树向上移动,直到到达其他设备。
在Python中,
stat可以按以下方式使用(未经测试,可能必须扩展以处理符号链接和诸如联合安装的奇特事物):
def find_mount_point(path): path = os.path.abspath(path) orig_dev = os.stat(path).st_dev while path != '/': dir = os.path.dirname(path) if os.stat(dir).st_dev != orig_dev: # we crossed the device border break path = dir return path
编辑 :
os.path.ismount直到现在我才知道。这大大简化了事情。
def find_mount_point(path): path = os.path.abspath(path) while not os.path.ismount(path): path = os.path.dirname(path) return path



