I want to delete several files which do not have the appropriate file extension (here .txt) from a dictionary. I intend to achieve this without using a for loop, but my implementation, which uses a combination of lambda and map function, for some reason does not delete the files. What am I doing wrong?
Code version without for loop (does not work):
files = next(os.walk(data_filepath))[2]
files_wrong_extension = list(filter(lambda x: not x.endswith(".txt"), files))
map(lambda x: os.remove(data_filepath + "/" + x), files_wrong_extension)
logger.info(f"{files_wrong_extension} were removed since they do not have a '.txt' file extension")
Code version with a for loop (works):
files = next(os.walk(data_filepath))[2]
files_wrong_extension = list(filter(lambda x: not x.endswith(".txt"), files))
for file in files_wrong_extension:
os.remove(data_filepath + "/" + file)
logger.info(f"{files_wrong_extension} were removed since they do not have a '.txt' file extension")