
Untitled
By: a guest on
Jul 22nd, 2012 | syntax:
None | size: 1.15 KB | hits: 10 | expires: Never
Python: removing multiple values from a list where using filters is more awkward
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
filtered = [x for x in os.listdir('.') if not any(fnmatch.fnmatch(x, p) for p in excluded)]
filtered = [x for x in os.listdir('.') if not re.search(r'.py~?$', x)]
excluded = ('.py', '.py~')
filtered = [x for x in os.listdir('.') if not x.endswith(excluded)]
for p in excluded:
fileNames = [filename for filename in fileNames if not fnmatch.fnmatch(filename, p)]
fileNames = [filename for filename in fileNames if not any(fnmatch.fnmatch(filename, p) for p in excluded)]
fileNames = [filename for filename in os.listdir() if not any(fnmatch.fnmatch(filename, p) for p in excluded)]
def includefilename(fileName):
for ex in excluded:
if fnmatch.fnmatch(fileName, ex):
return False
return True
fileNames = [fileName for fileName in fileNames if includefilename(fileName)]