I'm trying to use os.walk
to return a list of .xlsx files from a directory, but it's returning an empty list.
import os
import pathlib
working_directory = 'N:/files path'
def find_filenames(path, suffix):
# First save all filenames in a list: file_list:
file_list = []
# Gets a list of all validation xlsx files in the folder:
for subdir, dirs, files in os.walk(path):
for file in files:
if file.endswith(suffix):
# save filesnames in file_list:
file_list.append(file)
return file_list
print(find_filenames(pathlib.Path(working_directory), '.xlsx')
It's printing this:
[]
When it should look like this:
What am I missing or doing wrong? Thanks!
return
statement is indented incorrectly. logic is fine.xlsx
). Like in below answer from @raspiduino, the return needs to be done after looping over all files. You need to fix the indentation.