def getdirectorytree (Self,folder):
‘‘‘
:p Aram folder: File directory
: return: Dictionary of the Directory
‘‘‘
dirtree={' children ': []}
If Os.path.isfile (folder):
return {' text ': Os.path.basename (folder), ' icon ': ' Glyphicon glyphicon-leaf '}
Else
Basename=os.path.basename (folder)
dirtree[' text ']=basename
For item in Os.listdir (folder):
dirtree[' Children '].append (Self.getdirectorytree (Os.path.join (Folder,item)))
Return DirTree
def Getdirectorytreewithjson (Self,folder):
‘‘‘
Create a tree-like JSON string for a folder
:p Aram folder: Folders
: Return: File Tree JSON
‘‘‘
Return Json.dumps (Self.getdirectorytree (folder))
Result
{' text ': ', ' children ': [{' Text ': ' 1 ', ' icon ': ' Glyphicon glyphicon-leaf '}, {' Text ': ' 2 ', ' Icon ': ' Glyphicon glyphicon-le AF '}, {' text ': ' child01 ', ' children ': [{' Text ': ' child001 ', ' Children ': []}]}, {' text ': ' child02 ', ' Children ': []}]}
Web display:
<! DOCTYPE html>Results:
Traverse the directory tree to return to the dictionary tree, Directory Dictionary