Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 22nd, 2012  |  syntax: None  |  size: 1.15 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Python: removing multiple values from a list where using filters is more awkward
  2. import fnmatch
  3.  
  4. excluded = ['*.py', '*.py~']
  5.  
  6. fileNames = []
  7.  
  8. for fileName in os.listdir('.'):
  9.     fileNames.append(fileName)
  10.     print fileNames
  11.  
  12. for p in excluded:
  13.     if fnmatch.fnmatch(fileName, p):
  14.         fileNames.remove(fileName)
  15.         print fileNames
  16.        
  17. filtered = [x for x in os.listdir('.') if not any(fnmatch.fnmatch(x, p) for p in excluded)]
  18.        
  19. filtered = [x for x in os.listdir('.') if not re.search(r'.py~?$', x)]
  20.        
  21. excluded = ('.py', '.py~')
  22. filtered = [x for x in os.listdir('.') if not x.endswith(excluded)]
  23.        
  24. for p in excluded:
  25.     fileNames = [filename for filename in fileNames if not fnmatch.fnmatch(filename, p)]
  26.        
  27. fileNames = [filename for filename in fileNames if not any(fnmatch.fnmatch(filename, p) for p in excluded)]
  28.        
  29. fileNames = [filename for filename in os.listdir() if not any(fnmatch.fnmatch(filename, p) for p in excluded)]
  30.        
  31. def includefilename(fileName):
  32.     for ex in excluded:
  33.         if fnmatch.fnmatch(fileName, ex):
  34.             return False
  35.     return True
  36.  
  37. fileNames = [fileName for fileName in fileNames if includefilename(fileName)]