找到最深的嵌套路径?

发布于 2024-10-07 11:51:22 字数 166 浏览 0 评论 0原文

有没有办法用python找到最深的嵌套路径?

这样的目录列表

就像说如果你有一个像/cats/xmas/1.jpg /猫/海滩/2.jpg /dogs/xmas/2010/1.jpg

它将打印 /dogs/xmas/2010/1.jpg

是最长的路径

Is there a way to find the deepest nested path with python?

Like say if you had a list of directories like

/cats/xmas/1.jpg
/cats/beach/2.jpg
/dogs/xmas/2010/1.jpg

it would print
/dogs/xmas/2010/1.jpg

as being the longest path

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-10-14 11:51:22

def longest_path( paths ):
    key = lambda path:path.count('/')
    return max(paths, key=key)

你应该使用 os.path.normpath在计数之前的路径上。

我想在 Windows 上这可能会有点棘手,因为路径分隔符可以是 \ 或 / ...下面的代码让 os.path.split 弄清楚:

import os.path
def nesting(path):
    """ counts how often `os.path.split` works on `path` """
    c = 0
    head = tail = path
    while head and tail:
        head, tail = os.path.split(head)
        c +=1
    return c

def longest_path( paths ):
        return max(paths, key=nesting)

因为你正在寻找最深的路径,它必须是一个没有子文件夹的文件夹!你可以这样得到它:

def find_leafes( root ):
    """ finds folders with no subfolders """
    for root, dirs, files in os.walk(root):
        if not dirs: # can't go deeper
            yield root

print longest_path(find_leafes( root ))

Something like

def longest_path( paths ):
    key = lambda path:path.count('/')
    return max(paths, key=key)

You should use os.path.normpath on the paths before counting.

I guess on Windows this could get a bit tricky since the path separator can be either \ or / ... the code below lets os.path.split figure it out:

import os.path
def nesting(path):
    """ counts how often `os.path.split` works on `path` """
    c = 0
    head = tail = path
    while head and tail:
        head, tail = os.path.split(head)
        c +=1
    return c

def longest_path( paths ):
        return max(paths, key=nesting)

Since you're looking for the deepest path, it has to be a folder that has no subfolders! You can get it like this:

def find_leafes( root ):
    """ finds folders with no subfolders """
    for root, dirs, files in os.walk(root):
        if not dirs: # can't go deeper
            yield root

print longest_path(find_leafes( root ))
横笛休吹塞上声 2024-10-14 11:51:22

到目前为止,这似乎有效,

import os,sys

list = []
search_path = 'C:\Users\\Kevin\\Desktop\\'

def nesting(path):
    """ counts how often `os.path.split` works on `path` """
    c = 0
    head = tail = path
    while head and tail:
        head, tail = os.path.split(head)
        c +=1
    return c

def longest_path( paths ):
        return max(paths, key=nesting)

for root, dirs, files in os.walk(search_path):
   for name in files:       
       filename = os.path.join(root, name)
       sys.stdout.write('.')
       list.append(filename)

print longest_path(list)

非常感谢大家!

So far this seems to be working

import os,sys

list = []
search_path = 'C:\Users\\Kevin\\Desktop\\'

def nesting(path):
    """ counts how often `os.path.split` works on `path` """
    c = 0
    head = tail = path
    while head and tail:
        head, tail = os.path.split(head)
        c +=1
    return c

def longest_path( paths ):
        return max(paths, key=nesting)

for root, dirs, files in os.walk(search_path):
   for name in files:       
       filename = os.path.join(root, name)
       sys.stdout.write('.')
       list.append(filename)

print longest_path(list)

Thanks so much guys!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文