Search code examples
pythondirectory-structure

Traverse irregular list of list


I have a very irregular list of list containing a folder structure, and I want to iterate through the list and check whether that folder/sub-folder exist or not.

folderStructure = [['Folder1', [subfolder1, [sub-sub-folder1, sub-sub-folder2]]], ['Folder2', [sub-folder2], [sub-folder3]], ['Folder3', [sub-folder4]], ['Folder4'], [file1, file2, file3]]

How can I test if this folder structure exist?


Solution

  • In order to actually check if the folder exists, you have to specify its path and use os.path.exists. The difficult part is that the nested lists have strings that sometimes represent a folder's name, and other times a file name. I wrote a function that tests if the members of the supplied structure exist or not, and tries to determine if the content represents a folder name or not.

    import os
    folderStructure = [
        ['Folder1', 
            ['subfolder1', 
                ['sub-sub-folder1', 'sub-sub-folder2']
            ]
        ], 
        ['Folder2', 
            ['sub-folder2'], ['sub-folder3']
        ], 
        ['Folder3', 
            ['sub-folder4']
        ], 
        ['Folder4'], 
        ['file1', 'file2', 'file3']
    ]
    
    def path_hierarchy_exists(pathslist,base_path='.'):
        print pathslist,base_path
        if isinstance(pathslist,basestring): # pathslist is a string that names a file
            return os.path.exists(os.path.join(base_path,pathslist))
        elif len(pathslist)==1: # Leaf sub-folders or leaf files
            if not path_hierarchy_exists(pathslist[0],base_path):
                return False
        elif isinstance(pathslist[0],basestring) and isinstance(pathslist[1],list):
            # pathslist is a list starting with the folder name and following with a list of folder contents
            folderName = pathslist[0]
            if not os.path.exists(os.path.join(base_path,folderName)): # Folder does not exist
                return False
            for folderContents in pathslist[1:]:
                if not path_hierarchy_exists(folderContents,os.path.join(base_path,folderName)):
                    return False # Folder contents do not exist
        else: # pathslist is a list of nested folders
            for paths in pathslist:
                if not path_hierarchy_exists(paths,base_path):
                    return False
        return True
    
    print(path_hierarchy_exists(folderStructure))