Linked Questions

Popular Questions

Data Structure For Directory Tree in Python

Asked by At

I want to create a data structure that holds the full information for any arbitrary directory tree, addressed the by directory path. I envision something like this:

dirtree[ "/home/user" : { subdirs: [ "bin", "Desktop" ] , "files" : [ ".profile" , ".login" ]

or in a list form to have a initial point that is easily addressable:

dirtree[0][ "/home/user" : { subdirs: [ "bin", "Desktop" ] , "files" : [ ".profile" , ".login" ]

dirtree[1][ "/home/user/bin" : { subdirs: [ "Python" ] , "files" : [ "" , "" ]

Meaning that the items in the list would be a dict indexed by the full path of every directory, that contains two dicts, subdirs and files that both lists of the files and subdirectories in each directory.

I was thinking that the outer wrapper could be a list, so there is a starting point of the root of the directory tree in the 0th item, but I am not sure that is a must have. It would be quicker to traverse.

This is to hold all the information for a webserver

I got as far as a dict of lists, but how do I nest that inside another dist of directory full-paths?

>>> dir={ "subdirs":[] , "files":[] }
>>> dir
{'subdirs': [], 'files': []}

I am beginning with python, by the way.

I want to end up with something like dirtree{ "/home/username"}{"subdirs"}[] dirtree{ "/home/username"}{"files"}[]

Then I can start at the top and iterate down adding directories and files in as I find them, and keep the full directory subtree and be able to switch contexts to any directory in the sub-tree to generate a web page based on that for navigation in a web page.

Related Questions