import gc import os def GetAllPaths( raw_paths ): file_paths = [] paths_to_process = raw_paths while len( paths_to_process ) > 0: next_paths_to_process = [] for path in paths_to_process: if os.path.isdir( path ): subpaths = [ os.path.join( path, filename ) for filename in os.listdir( path ) ] next_paths_to_process.extend( subpaths ) else: file_paths.append( path ) paths_to_process = next_paths_to_process gc.collect() return file_paths