Create a graphical tree of directories and files in python

0

Greetings I would like to get a graphic type tree from my folders and their respective files with python I currently have the following code:

rootDir = 'media/gestionDocumental/'
directorioArr=[]
for dirName, subdirList, fileList in os.walk(rootDir):
    #print('Found directory: %s' % dirName)
    nombreAbsoluto=dirName.split("/")
    directorioArr.append(nombreAbsoluto[2])
    for fname in fileList:
        #print('\t%s' % fname)
        directorioArr.append(fname)

which gives me the following result:

How can I improve this method .. Thanks in advance!

    
asked by Diego Avila 04.04.2018 в 16:16
source

1 answer

0

I answer the question in case someone needs it:

First of all we need to know the path or rootDir:

rootDir = "mi/directorio/root"

then with this we make a tour using os.walk something like this:

list_dir=[]
rootDir = "mi/directorio/root"
            for dirName, subdirList, fileList in os.walk(rootDir):
                element={}

                #validamos que no vaya la carpeta root en mi caso no lo deseo
                if dirName != 'media/gestionDocumental/':
                    #nombre original para mostrar 
                    element['carpeta'] = dirName.split('media/gestionDocumental/')
                    #url/ruta esta es la ruta absoluta
                    element['url_carpeta'] = dirName

                    #archivos cada uno de los archivos pertenecientes al folder
                    element['archivos'] = fileList
                    #agregamos element al array
                    list_dir.append(element) 

with this we would already have everything in list_dir, visually we would have something like this:

    
answered by 21.06.2018 / 22:29
source