确定子目录是否在安装了Python的文件系统上

2024-06-16 11:05:21 发布

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

在这个路径中,目录'foo'安装在外部文件系统上。“bar”目录是“foo”的子目录。在

'/Volumes/foo/bar'

使用os.path.ismount('/Volumes/foo'),我可以正确地确定'foo'确实是一个外部挂载。但是,在'bar'上使用os.path.ismount('/Volumes/foo/bar')可以正确地确定它不是外部安装的。在

所以,我的问题是,如何才能正确地确定'bar'是外部安装的文件系统的子目录?我需要能够确定相同的许多不同深度的目录。任何线索都太好了!在


Tags: path路径目录fooosbarvolumes线索
2条回答

文件来源:

Return True if pathname path is a mount point

强调我的。装载指向目录的子目录位于装载驱动器上,但不是装载“点”。在

how can I correctly determine that 'bar' is a subdirectory of an externally mounted file system?

在这种情况下,我会迭代到一个根,或者到达一个点。先到者为准。在

假设是Unix类型的文件系统:

def is_on_mount(path):
  while True:
    if path == os.path.dirname(path):
      # we've hit the root dir
      return False
    elif os.path.ismount(path):
      return True
    path = os.path.dirname(path)

path = '/mount/one/two/three'
is_on_mount(path)
import os
import subprocess


def is_on_mounted_volume(path):
    try:
        df = subprocess.check_output(['df', path]).split('\n')
        mountpoint = df[1].split()[-1][0]
        return os.path.ismount(mountpoint)
    except subprocess.CalledProcessError:
        pass

相关问题 更多 >