Find all files that match a filename pattern in a folder tree I am an old python newbie here, migrating from fortran77 ;) 
Still unsure of the python terminology at the moment :(

The python3 script below fetches a large list of matching files in a folder tree, in the format:

[ fully_qualified_folder_name, [list of matching files in this folder] ]


Being only a python newbie, there HAS to be a better way ?

I hope to use the code to generate a nightly json file underpinning a chromecasting web server on a Raspberry Pi 4, based on Google's example chrome (web page) "sender". 
The web page will eventually use javascript on the client-side for the user to choose a file from a list and "cast" it from the Pi4's apache2 web server over to the chromecast device. 
So, I thought, why not give try Python a try ?

Suggestions would be appreciated.

import os
import fnmatch
def yield_files_with_extensions(folder_path, file_match):
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if fnmatch.fnmatch(file.lower(),file_match.lower()):
                #yield os.path.join(root, file)
                yield file
        break  # without this line it traverses the subfolders too
def yield_files_in_subfolders(folder_path, file_match):
    for root, dirs, files in os.walk(folder_path):
        for d in dirs:
            subfolder = os.path.join(root, d)
            mp4_files = [subfolder, [f for f in yield_files_with_extensions(subfolder, file_match)]]
            yield mp4_files
#
the_path = r'/mnt/mp4library/mp4library'
# retieve the topmost folder's files
mp4_files1 = [[ the_path, [f for f in yield_files_with_extensions(the_path, "*.mp4")] ]]
mp4_files2 = [f for f in yield_files_in_subfolders(the_path, "*.mp4")]
mp4_files2 = mp4_files1 + mp4_files2
crecords=0
cfiles=0
for a in mp4_files2:
    crecords=crecords+1
    print ("-----record "+str(crecords)+"---" + a[0] + " ... files=" + str(len(a[1])))
    #print (a[0])
    #print (a[1])
    c=0
    for b in a[1]:
        c=c+1
        print ("mp4 File "+str(c)+"---" + b)
    cfiles=cfiles+c
print ('Count of files: ' + str(cfiles))
As you tagged this Python 3.x, I'd suggest using pathlib.Path.rglob() and collections.defaultdict.  Also, check out f-strings.

import collections
import pathlib

mp4s = collections.defaultdict(list)

root = pathlib.Path(r'/mnt/mp4library/mp4library')

file_count = 0

for filepath in root.rglob('*.mp4'):
    #filepath is relative to root. Uncomment the next line for absolute paths
    #filepath = filepath.resolve()

    mp4s[filepath.parent].append(filepath.name)

    file_count += 1

for record_number, (folder, filenames) in enumerate(sorted(mp4s.items())):

    print(f"-----record {record_number}---{folder} ... files={len(filenames)}")

    for c,filename in enumerate(filenames):
        print(f"mp4 File {c}---filename")

print('Count of files: {filecount}')