'Walk inventory objects one position of the searchpath at a time'

def find_folder(self, searchpath):

    paths = [x.replace('/', '') for x in searchpath.split('/')]
    paths_total = (len(paths) - 1)
    position = 0
    root = self.content.rootFolder
    while (root and (position <= paths_total)):
        change = False
        if hasattr(root, 'childEntity'):
            for child in root.childEntity:
                if (child.name == paths[position]):
                    root = child
                    position += 1
                    change = True
                    break
        elif isinstance(root, vim.Datacenter):
            if hasattr(root, 'vmFolder'):
                if (root.vmFolder.name == paths[position]):
                    root = root.vmFolder
                    position += 1
                    change = True
        else:
            root = None
        if (not change):
            root = None
    return root
