Это кажется слишком сложным. Посмотрите этот подход с os.walk и списком-фильтрами для фильтрации файлов:
import os
Создание файлов:
dirs = [r"./subdir",r"./subdir/tata",r"subdir/tarumpa",r"./dir2b"]
files = ["k.mp4","some.txt","cool.mp3"]
def touch(p,fn):
with open(os.path.join(p,fn),"w") as f:
f.write(" ")
for d in dirs:
os.mkdir(d)
for f in files:
touch(d,f)
Поиск файлов:
movie = []
music = []
# os.walk recurses into subdirectories, it returns a generator for each directory
# including the directory its in as root, all subdirs in dirs and all files in
# files: then it steps into each of the dirs and does the same ...
for root,dirs,files in os.walk("./"):
# root is the dir we are currently in, f the filename that ends on ...
movie.extend( (os.path.join(root,f) for f in files if f.endswith(".mp4")) )
music.extend( (os.path.join(root,f) for f in files if f.endswith(".mp3")) )
print(movie)
print(music)
Выход:
# movies
['./subdir/k.mp4', './subdir/tarumpa/k.mp4', './subdir/tata/k.mp4', './dir2b/k.mp4']
# music
['./subdir/cool.mp3', './subdir/tarumpa/cool.mp3',
'./subdir/tata/cool.mp3', './dir2b/cool.mp3']