Home > Software engineering >  Python get a list of files if they are one of two types
Python get a list of files if they are one of two types

Time:11-03

This should be very simple. I want to get a list of all files which are either json or xlsm

    filelist = os.listdir(path='.')
    jsonlist = [s for s in filelist if (".json" or ".xlsm") in s]

The above gives me a list of jsons only. If I remove the () I get all files in the folder and if I replace "or" with "and" I get xlsm only. How do I correct this?

CodePudding user response:

Your condition formating is not correct:
jsonlist = [s for s in filelist if (".json" in s) or (".xlsm" in s)]

  • Related