Trying to create a list of files with multiple extensions over which to iterate. Most of the answers on stack overflow involve filtering using lambda but I’m not sure how this can work in this case (just because of the way the iteration would be set up). So far I have
import fnmatch
excluded = ['*.py', '*.py~']
fileNames = []
for fileName in os.listdir('.'):
fileNames.append(fileName)
print fileNames
for p in excluded:
if fnmatch.fnmatch(fileName, p):
fileNames.remove(fileName)
print fileNames
Obviously the problem is that list.remove only removes the first instance and not all instances. What do you reckon would be the most efficient way to solve this problem?
Thanks!
Use a list comprehension )):
Alternatively, a more compact code using regular expressions:
or simply use
endswith