找到最深的嵌套路径?
有没有办法用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像
你应该使用
os.path.normpath
在计数之前的路径上。
我想在 Windows 上这可能会有点棘手,因为路径分隔符可以是 \ 或 / ...下面的代码让 os.path.split 弄清楚:
因为你正在寻找最深的路径,它必须是一个没有子文件夹的文件夹!你可以这样得到它:
Something like
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:Since you're looking for the deepest path, it has to be a folder that has no subfolders! You can get it like this:
到目前为止,这似乎有效,
非常感谢大家!
So far this seems to be working
Thanks so much guys!