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 ?
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 + " ... files=" + str(len(a))) #print (a) #print (a) c=0 for b in a: c=c+1 print ("mp4 File "+str(c)+"---" + b) cfiles=cfiles+c print ('Count of files: ' + str(cfiles))