once you get the full path to your file using directory, just use get_extension()
example below
func dircontents(path):
var dir = Directory.new()
if dir.open(path) == OK:
dir.listdirbegin()
var filename = dir.getnext()
while filename != "":
if dir.currentisdir():
print("Found directory: " + filename)
else:
print("Found file: " + filename)
filename = dir.getnext()
print(filename.getextension())
else:
print("An error occurred when trying to access the path.")
func onButtonpressed():
dircontents("res://sounds/music");