创建一个类而不是一个字典值得吗?写了一个简短的,应该做你想做的事
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 "}"
然后您可以输入第一个路径并保存对它的引用
myFileSystem = FileSystem("base/pictures/whatever.png")
这个myFileSystem 将是您对“基础”级别的引用,使用它和它的方法您应该能够做您想做的事。
然后,当您有第二条路径要添加时,您必须通过在 myFileSystem 上使用 getChildren() 找到正确的节点来添加它,直到找到差异,然后使用 addChild() 添加其余的该节点的文件路径。
然后使用myFileSystem.printAllChildren() 会打印出整个文件系统。
-------编辑-------
对我写了一半的代码不太满意,喜欢挑战,所以这里有一个易于使用的课程
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()
正如您在最后看到的那样,当我简单地执行myFiles.addChild(record) 时,addChild 函数现在负责在树中找到正确的位置供它进入。 printAllChildren() 至少为那些提供了正确的输出参数。
如果其中任何一个没有意义,请告诉我,就像我说它没有经过全面测试,所以一些极端情况(例如,尝试添加另一个基础?)可能会让它变得很奇怪。
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()