目录的python n-ary树

时间:2012-06-20 16:00:52

标签: python tree

我正在python中搜索一个代表目录的n-ary树的答案。 我认为文件夹的N-ary树就是一个很好的例子。这是我的代码:

class Node(object):
    def __init__(self, data):
        self.data = data
        self.children = []

n = Node(sys.argv[1])

def walktree(top): # recurse from "/top/"

    X = [] # clear record
    for f in os.listdir(top):
        pathname = os.path.join(top,f)
        mode = os.stat(pathname)[ST_MODE]
        if S_ISDIR(mode):  # It's a directory
            X.append(pathname)  #
            walktree(pathname)  # recursion
    X.sort()
    n.add_child(X)

我的问题是:

  1. 这段代码是否正确?
  2. 如何填充N-ary树的给定节点
  3. 如何遍历构建的N-ary树
  4. 对1,2,3的答案。

    def dirwalk(top): # build an n_ary tree 
        n = Node(top) # ONE Node per folder 
        for f in os.listdir(top): # loop over the current dir
            pathname = os.path.join(top,f) # dir + fname
            mode = os.stat(pathname)[ST_MODE]
            if S_ISDIR(mode):  # It's a directory
                ptr = dirwalk(pathname) # go down 
                n.add_child(ptr) # add pointer to node
        return n # return Node
    
    def traverse(n): # traverse buildt tree
        for p in n:  # loop over children
            print p.data  
            traverse(p.children)
    

2 个答案:

答案 0 :(得分:0)

在我看来,这样做的pythonic方法就是使用嵌套列表。您可以通过元组(subdirs, files)表示目录,其中subdirsfiles都是列表,subdirs包含更多元组,files是文件名列表。< / p>

构建示例,包含Niklas B建议的命名元组:

import os, os.path
from collections import namedtuple

Directory = namedtuple('Directory', ['path', 'subdirectories', 'files'])

def walktree(path):
    dirs = []
    files = []
    for f in os.listdir(path):
        if os.path.isdir(os.path.join(path, f)):
            dirs.append(walktree(os.path.join(path, f)))
        else:
            files.append(f)
    return Directory(path, dirs, files)

答案 1 :(得分:0)

使用os.walk实现这一点应该是微不足道的:

class Node():
    def __init__(self, name):
        self.name = name
        self.files = []
        self.folders = {} 
    def __str__(self):
        return '%s: files: %s, folders: %s' % (self.name, self.files, self.folders)

def find_node(top_node, name):
    import pdb; pdb.set_trace()
    result = top_node
    path = name.split(os.sep)[1:]
    for path_comp in path:
        result = result.folders[path_comp]

    return result

import sys, os
tree = Node(sys.argv[1])
for root, folders, files in os.walk(sys.argv[1]):
    parent_node = find_node(tree, root)

    for folder in folders:
        parent_node.folders[folder] = Node(folder)

    for file in files:
        parent_node.files.append(file)

print tree