-1

重複の可能性:
Python でツリー形式のディレクトリ リストを作成する

ファイル システムを分析し、結果を書式付きファイルとして出力したいと考えています。私の最初の実装は単純なプレーン テキストですが、後で HTML を組み込みたいと考えています。

基本的な方法を使用して、各フォルダーに含まれるファイルを収集しています。これらのファイルをテキスト プロセッサに返したり送信したりできます。

def gather_tree(source):
        tree = {}
        for path, dirs, files in os.walk(source):
            tree[path] = []
            for file in files:
                tree[path].append(file)
        return tree

明らかに、ここでの問題は、十分なスペースとネストを使用してリストを正しくフォーマットできる必要があると思われる深さの概念を持たない構造を作成していることです。

私の現在の非常に基本的な印刷パターンは次のようになります。

def print_file_tree(tree):
    # Prepare tree
    dir_list = tree.keys()
    dir_list.sort()

    for directory in dir_list:
        print directory
        for f in tree[directory]:
            print f

私はデータ構造が初めてなので、いくつかの情報をいただければ幸いです。

4

1 に答える 1

2

データから XML を作成する場合は、実際にはツリー状の構造を作成する必要があります。ElementTreeこれは、構築して注釈を付け、場合によっては反復またはトラバースしてから、 XML を作成するために に変換する中間ツリーにすることができます。または、 lxmlの ElementTree APIElementTreeを使用して直接構築することもできます。

いずれにせよ、使用os.walk()する方法はありません。これは直感に反するように聞こえるかもしれませんが、要点は次のとおりos.walk()です。ファイル システム ツリーをシリアル化 (フラット化) するので、簡単に反復処理を行うことができ、それを行う再帰関数を記述する必要はありません。ただし、あなたの場合、ツリー構造を保持したいので、その再帰関数を自分で記述した方がはるかに簡単です。

ElementTreeこれは、を使用してを構築する方法の例ですlxml

(このコードは、同様の質問に対する @MikeDeSimone の回答に大まかに基づいています)

import os
from lxml import etree


def dir_as_tree(path):
    """Recursive function that walks a directory and returns a tree
    of nested etree nodes.
    """
    basename = os.path.basename(path)
    node = etree.Element("node")
    node.attrib['name'] = basename
    # Gather some more information on this path here
    # and write it to attributes
    # ...
    if os.path.isdir(path):
        # Recurse
        node.tag = 'dir'
        for item in sorted(os.listdir(path)):
            item_path = os.path.join(path, item)
            child_node = dir_as_tree(item_path)
            node.append(child_node)
        return node
    else:
        node.tag = 'file'
        return node

# Create a tree of the current working directory
cwd = os.getcwd()
root = dir_as_tree(cwd)

# Create an element tree from the root node
# (in order to serialize it to a complete XML document)
tree = etree.ElementTree(root)

xml_document = etree.tostring(tree,
                              pretty_print=True,
                              xml_declaration=True,
                              encoding='utf-8')
print xml_document

出力例:

<?xml version='1.0' encoding='utf-8'?>
<dir name="dirwalker">
  <dir name="top1">
    <file name="foobar.txt"/>
    <dir name="sub1"/>
  </dir>
  <dir name="top2">
    <dir name="sub2"/>
  </dir>
  <dir name="top3">
    <dir name="sub3">
      <dir name="sub_a"/>
      <dir name="sub_b"/>
    </dir>
  </dir>
  <file name="topfile1.txt"/>
  <file name="walker.py"/>
</dir>
于 2012-09-26T22:48:04.517 に答える