Could it be worth making a class rather than a dict? Wrote up a short one that should do what you want
class FileSystem():
def __init__(filePath=None):
self.children = []
if files != None:
try:
self.name, child = files.split("/", 2)
self.children.append(FileSystem(filePath))
except (ValueError):
pass
def addChild(filePath):
self.children.append(FileSystem(filePath))
def getChildren():
return self.children
def printAllChildren():
print "Name: "+ self.name
print "{ Children:"
for child in self.children:
child.printAllChildren()
print "}"
You could then enter the first path and save a reference to it like
myFileSystem = FileSystem("base/pictures/whatever.png")
This myFileSystem will be your reference to the "base" level, and using that and it's methods you should be able to do what you want.
And then when you have a second path to add you would have to find the correct node to add it to by using getChildren() on myFileSystem until you find a discrepancy, then use addChild() to add the rest of the filepath to that node.
Then using myFileSystem.printAllChildren() will print out the whole file system.
-------EDIT-------
Wasn't too happy with my half written code and liked the challenge so here is a easy to use class
class FileSystem():
def __init__(self,filePath=None):
self.children = []
if filePath != None:
try:
self.name, child = filePath.split("/", 1)
self.children.append(FileSystem(child))
except (ValueError):
self.name = filePath
def addChild(self, filePath):
try:
thisLevel, nextLevel = filePath.split("/", 1)
try:
if thisLevel == self.name:
thisLevel, nextLevel = nextLevel.split("/", 1)
except (ValueError):
self.children.append(FileSystem(nextLevel))
return
for child in self.children:
if thisLevel == child.name:
child.addChild(nextLevel)
return
self.children.append(FileSystem(nextLevel))
except (ValueError):
self.children.append(FileSystem(filePath))
def getChildren(self):
return self.children
def printAllChildren(self, depth = -1):
depth += 1
print "\t"*depth + "Name: "+ self.name
if len(self.children) > 0:
print "\t"*depth +"{ Children:"
for child in self.children:
child.printAllChildren(depth)
print "\t"*depth + "}"
records = ["base/images/graphs/one.png", "base/images/tikz/two.png",
"base/refs/images/three.png", "base/one.txt", "base/chapters/two.txt"]
myFiles = FileSystem(records[0])
for record in records[1:]:
myFiles.addChild(record)
myFiles.printAllChildren()
As you can see at the end when i simply do myFiles.addChild(record), the addChild function now takes care of finding the right place in the tree for it to go in. The printAllChildren() gives the correct output at least for those parameters.
Let me know if any of it doesnt make sense, like I said its not fully tested so some corner cases (e.g. trying to add another base?) might make it go weird.
EDIT2
class FileSystem():
def __init__(self,filePath=None):
self.children = []
if filePath != None:
try:
self.name, child = filePath.split("/", 1)
self.children.append(FileSystem(child))
except (ValueError):
self.name = filePath
def addChild(self, filePath):
try:
thisLevel, nextLevel = filePath.split("/", 1)
try:
if thisLevel == self.name:
thisLevel, nextLevel = nextLevel.split("/", 1)
except (ValueError):
self.children.append(FileSystem(nextLevel))
return
for child in self.children:
if thisLevel == child.name:
child.addChild(nextLevel)
return
self.children.append(FileSystem(nextLevel))
except (ValueError):
self.children.append(FileSystem(filePath))
def getChildren(self):
return self.children
def printAllChildren(self, depth = -1):
depth += 1
print "\t"*depth + "Name: "+ self.name
if len(self.children) > 0:
print "\t"*depth +"{ Children:"
for child in self.children:
child.printAllChildren(depth)
print "\t"*depth + "}"
def makeDict(self):
if len(self.children) > 0:
dictionary = {self.name:[]}
for child in self.children:
dictionary[self.name].append(child.makeDict())
return dictionary
else:
return self.name
records = ["base/images/graphs/one.png", "base/images/tikz/two.png",
"base/refs/images/three.png", "base/one.txt", "base/chapters/two.txt"]
myFiles = FileSystem(records[0])
for record in records[1:]:
myFiles.addChild(record)
print myFiles.makeDict()