Jul-29-2020, 04:22 PM
I also think there are times to use list comprehension and times when it is better to use a loop. This is ugly:
This is longer, but it is easier to read:
files = [file for file in os.listdir(input_dir) if os.path.isfile(file) and file.endswith(".xlsx")]I know it is ugly because later on you used os.path.join so you knew it was needed. The only reason you didn't use it in the list comprehension is that the ungainly syntax hid the need.
This is longer, but it is easier to read:
files = [] for file in os.listdir(input_dir) file = os.path.join(input_dir, file) if os.path.isfile(file) and file.endswith(".xlsx"): files.append(file)And when you are done with the loop you have a list of filenames with paths.