如何在python中解析目录树?

2024-05-20 05:47:08 发布

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

我有一个名为“笔记”的目录,在笔记中,我有被命名为“科学”,“数学”。。。在这些文件夹中有子类别,如“量子力学”、“线性代数”。

./notes
--> ./notes/maths
------> ./notes/maths/linear_algebra
--> ./notes/physics/
------> ./notes/physics/quantum_mechanics
我的问题是,我不知道如何将类别和子类别分成两个单独的列表/数组。


Tags: 目录文件夹数学科学类别命名notes笔记
0条回答
网友
1楼 · 发布于 2024-05-20 05:47:08

你可以利用^{}

#!/usr/bin/env python

import os
for root, dirs, files in os.walk('notes'):
    print root, dirs, files

简单的两级遍历:

import os
from os.path import isdir, join

def cats_and_subs(root='notes'):
    """
    Collect categories and subcategories.
    """
    categories = filter(lambda d: isdir(join(root, d)), os.listdir(root))
    sub_categories = []
    for c in categories:
        sub_categories += filter(lambda d: isdir(join(root, c, d)), 
            os.listdir(join(root, c)))

    # categories and sub_categories are arrays,
    # categories would hold stuff like 'science', 'maths'
    # sub_categories would contain 'Quantum Mechanics', 'Linear Algebra', ...
    return (categories, sub_categories)

if __name__ == '__main__':
    print cats_and_subs(root='/path/to/your/notes')
网友
2楼 · 发布于 2024-05-20 05:47:08

os.walk非常适合这个。默认情况下,它将执行自上而下的遍历,您可以通过将“dirnames”设置为空,在第二级轻松终止它。

import os
pth = "/path/to/notes"
def getCats(pth):
    cats = []
    subcats = []
    for (dirpath, dirnames, filenames) in os.walk(pth):
        #print dirpath+"\n\t", "\n\t".join(dirnames), "\n%d files"%(len(filenames))
        if dirpath == pth:
            cats = dirnames
        else:
            subcats.extend(dirnames)
            dirnames[:]=[] # don't walk any further downwards
    # subcats = list(set(subcats)) # uncomment this if you want 'subcats' to be unique
    return (cats, subcats)

相关问题 更多 >