Here's the scenario:
I have a long list of time-stamped file names with characters before and after the time-stamp.
Something like this:
['prefix1', '2016', 'suffix']
for each fileName in the master list:
if the fileName contains EVERY element in the "contains" array:
add fileName to filtered list of filenames
I'd compile the list into a
import fnmatch pattern = '*'.join(contains) filetered_filenames = fnmatch.filter(master_list, pattern)
This basically concatenates all strings in
contains into a glob pattern with
* wildcards in between. This assumes the order of
contains is significant. Given that you are looking for prefixes, suffixes and (parts of) dates in between, that's not that much of a stretch.
It is important to note that if you run this on an OS that has a case-insensitive filesystem, that
fnmatch matching is also case-insensitive. This is usually exactly what you'd want in that case.