Possible Duplicate:
Create a tree-style directory listing in Python
I would like to analyze the file system and print the result as formatted file. My first implementation can simply be plain text, but at a later date I would like to incorporate HTML.
I am using a basic method to gather the files contained within each folder, which I can return and send to the text processor:
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
Clearly, the problem here is that I am producing a structure that has no concept of depth which I guess I need to be able to correctly format the list with adequate space and nesting.
My currently very basic print pattern looks like this:
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
I am kind new to data structures and would appreciate some input!