def find_filegroups(paths, substring='', extensions=None, validity_check=True,
                    ignore_invisible=True, rstrip='', ignore_substring=None):
    n = len(paths)

    # must have same number of paths and extensions
    assert(len(paths) >= 2)
    if extensions:
        assert(len(extensions) == n)
    else:
        extensions = ['' for i in range(n)]

    base = find_files(path=paths[0],
                      substring=substring,
                      check_ext=extensions[0],
                      ignore_invisible=ignore_invisible,
                      ignore_substring=ignore_substring)
    rest = [find_files(path=paths[i],
                       substring=substring,
                       check_ext=extensions[i],
                       ignore_invisible=ignore_invisible,
                       ignore_substring=ignore_substring) for i in range(1, n)]

    groups = {}
    for f in base:
        basename = os.path.splitext(os.path.basename(f))[0]
        basename = re.sub('\%s$' % rstrip, '', basename)
        groups[basename] = [f]

    # groups = {os.path.splitext(os.path.basename(f))[0].rstrip(rstrip):[f]
    #           for f in base}

    for idx, r in enumerate(rest):
        for f in r:
            basename, ext = os.path.splitext(os.path.basename(f))
            basename = re.sub('\%s$' % rstrip, '', basename)
            try:
                if extensions[idx+1] == '' or ext == extensions[idx+1]:
                    groups[basename].append(f)
            except KeyError:
                pass

    if validity_check:
        lens = [len(groups[k]) for k in groups.keys()]
        if len(set(lens)) > 1:
            raise ValueError('Warning, some keys have more/less values than'
                             ' others. Set validity_check=False'
                             ' to ignore this warning.')

    return groups
Find and collect files from different directories in a python dictionary.
dataset/ETHPy150Open rasbt/mlxtend/mlxtend/file_io/find_filegroups.py/find_filegroups