Traverse irregular list of list

94 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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))
6
On

I'm not sure what the subfolders are, but this will work if you are looking for a string in a irregularly shaped array. You should try and understand what this is doing by reading about depth first search.

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

def searchFolder(folder, name):
    for item in folder:
        if isinstance(item, basestring):
            if item == name:
                return True
        elif searchFolder(item, name):
            return True

    return False

print searchFolder(folderStructure, 'Folder4')

Pass the folder Structure as the first parameter and the name of the folder that you are searching for as the second.