Find files in directories

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Timofffee
:warning: Old Version Published before Godot 3 was released.

Help! This code works in the editor but does not work in the assembled project!

var path = "res://blocks"
var paths = []
var d = Directory.new()
if d.open( path )==0:
	d.list_dir_begin()
	var file_name = d.get_next()
	while(file_name!=""):
		if d.current_is_dir():
			if (file_name != "." and file_name != ".."):
				print("Found block directory: " + file_name)
				paths.append(path+"/"+file_name)
		else:
			if (file_name.extension() == "res"):
				print("Found block: " + file_name.basename())
				blocks[file_name.basename()] = load(path+"/"+file_name.basename()+".res")
		file_name = d.get_next()
else:
	print("Some open Error, maybe directory not found? (blocks)")
if (paths.size() > 0):
	for i in range (0 , paths.size()):
		var d = Directory.new()
		print (paths[i]) 
		if d.open( paths[i] )==0:
			d.list_dir_begin()
			var file_name = d.get_next()
			while(file_name!=""):
				if d.current_is_dir():
					if (file_name != "." and file_name != ".."):
						print("Found block directory: " + file_name)
				else:
					if (file_name.extension() == "res"):
						print("Found block: " + file_name.basename())
						blocks[file_name.basename()] = load(paths[i]+"/"+file_name.basename()+".res")
				file_name = d.get_next()
:bust_in_silhouette: Reply From: volzhs

This is working code from my project.

func list_files_in_directory(path):
	var files = []
	var dir = Directory.new()
	dir.open(path)
	dir.list_dir_begin()
	
	while true:
		var file = dir.get_next()
		if file == "":
			break
		elif not file.begins_with(".") and file.extension() == "scn":
			files.append(file)
	
	dir.list_dir_end()
	
	return files

and I call it like list_files_in_directory("res://stages/")