0

我在 python 中寻找一个答案来表示目录的 n 叉树。我认为文件夹的 N 叉树就是一个很好的例子。这是我的代码:

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叉树的给定节点
  3. 如何遍历构造的 N 叉树

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)
4

2 回答 2

0

This should be fairly trivial to implement using 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
于 2012-06-20T16:45:02.030 回答
0

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

构造示例,合并了 Niklas B 建议的 namedtuple:

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)
于 2012-06-20T16:08:19.097 回答