How to loop through list of options inside if statement in python 2.7? -
i extracting items subdirectory containing mixture of files audio files in different formats , different suffixes e.g. _master or _128k.
i have specified higher in code list of permitted extensions (e.g. .mp3) extract files of right formats processing.
i have list (suffixexcluded) containing filename suffixes (e.g. _syndication) explicitly want exclude further processing.
how best write line does:
if fileextension in filesallowed , [list of excluded suffixes] not in filename: is there neat, compact , elegant (pythonic) way of iterating through list of exclusions within if clause, or need set subsidiary loop test each item?
you can filter go, passing tuple of extensions want keep , filtering remove files matching extensions don't contain substring list of excluded substrings.
exc = [list of excluded suffixes] import os f in os.listdir("path"): if f.endswith((".mp4",".mp3",".avi")) , not any(e in f e in exc): you need single pass on directory content without need build list first.
if want replace forbidden substrings , not exclude can use re.sub:
import os import re r = re.compile(r"|".join([e e in exc])) f in os.listdir("path"): if f.endswith((".mp4",".mp3",".avi")): f = r.sub("",f)
Comments
Post a Comment