如何确定路径是否是另一个的子目录? [英] How to determine if a path is a subdirectory of another?

查看:285
本文介绍了如何确定路径是否是另一个的子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出了我需要检查其中文件的路径的列表.当然,如果给我一个根目录和一个子目录,则无需处理该子目录.例如

I am given a list of paths that I need to check files within. Of course, if I am given a root, and a subdirectory, there is no need to process the sub-directory. For example

c:\test  // process this
c:\test\pics // do not process this
c:\test2 // process this

我该如何(跨平台)告诉您,路径不是另一个路径的子目录.最好是我希望它是跨平台的,并且只要符号链接不是周期性的,就不必担心它们(更糟糕的是,我最终将数据处理了两次).

How can I tell (cross platform) that a path is not a subdirectory of the other. Preferably I would want this to be cross platform, and am not worried about symlinks as long as they are not cyclical (worse case is that I end up processing the data twice).

更新:这是我最后使用的代码,这要感谢@ F.J

UPDATE: here is the code I ended up using, thanks to @F.J

   def unique_path_roots(paths):
    visited = set()
    paths = list(set(paths))

    for path in sorted(paths,key=cmp_to_key(locale.strcoll)):
        path = normcase(normpath(realpath(path)))

        head, tail = os.path.split(path)
        while head and tail:
            if head in visited:
                break
            head, tail = os.path.split(head)
        else:
            yield path
            visited.add(path)

推荐答案

我将维护一组您已经处理过的目录,然后针对每个新路径检查该目录中是否已存在其父目录.处理中:

I would maintain a set of directories you have already processed, and then for each new path check to see if any of its parent directories already exist in that set before processing:

import os.path

visited = set()
for path in path_list:
    head, tail = os.path.split(path)
    while head and tail:
        if head in visited:
            break
        head, tail = os.path.split(head)
    else:
        process(path)
        visited.add(path)

请注意,应该对path_list进行排序,以便子目录(如果存在)始终位于其父目录之后.

Note that path_list should be sorted so that subdirectories are always after their parent directories if they exist.

这篇关于如何确定路径是否是另一个的子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆