2012-09-26 48 views
0

可能重複:
Create a tree-style directory listing in Python獲取文件系統上市的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 

我一種新的數據結構,並希望一些輸入!

+0

嗯,你的樹是不是一個真正的樹,因爲你已經注意到:)樹由**節點組成**有**父母**和**孩子**。維基百科關於[圖論](http://en.wikipedia.org/wiki/Graph_theory)的文章可能是第一個好讀物。但是,什麼數據結構最適合取決於你想要對數據做什麼 - 也許你甚至不需要樹。 你的文本處理器會做什麼樣的工作? –

+0

首先,我希望能夠以合適的格式打印文件,例如XML和HTML。然後,我想收集有關lising中包含的文件類型的信息。 –

+0

然後這似乎是密切相關的[這個問題](http://stackoverflow.com/questions/2104997/os-walk-python-xml-representation-of-a-directory-structure-recursion)(雖然我會使用[lxml](http://lxml.de/)來生成XML樹)。 –

回答

2

如果您計劃從您的數據創建XML,你實際上將不得不創建一個樹狀結構。這可以是一箇中間樹,您可以構建,註釋並可能重複或遍歷,然後轉換爲用於創建XML的ElementTree。或者您可以使用lxml的ElementTree API直接構建ElementTree

無論哪種方式,使用os.walk()是不是要走的路。這聽起來可能與直覺相反,但重點是:os.walk()序列化(展平)文件系統樹,以便您可以輕鬆地迭代它,而不必處理編寫這樣做的遞歸函數。在你的情況下,你要想要保留樹結構,因此如果你自己編寫遞歸函數會容易得多。

這是如何使用lxml建立一個ElementTree一個例子。

(此代碼是鬆散的基礎上@MikeDeSimone's answer到類似的問題)

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>