lundi 26 novembre 2018

Python - If statement to check against a range of choices an

I iterate across a list of files and want to check if they have one of many extensions before processing. Is it possible to accomplish this avoiding both these scenarios ? - Checking for each extension through an OR statement (i.e. hard-coding) - A loop within a loop

Construct so far:

#Iterate over files
for filename in os.listdir(os.getcwd()+dataDir):
    #iterate over ext (want to avoid)
    for u in ['jpg','txt'] :
        if filename.endswith(u):
            print("An ext of my choice found!")
        continue
    else:
        continue

Thanks in advance. regards, Sundaresh

Aucun commentaire:

Enregistrer un commentaire