0

How do I write this correctly:

for root, dirs, files in os.walk(SEARCHDIR):
    files = [f for f in files if not f[0] == '.' and not ("BLACKBOX" or ".RTN" or ".log") in f]

I want to achieve my file list to contain all files instead of hidden files or files conaining one of the strings "BLACKBOX" or ".RTN" or ".log". Anyway only the first expression is evaluated ("BLACKBOX") the others are ignored.

Stefatronik
  • 312
  • 2
  • 16

3 Answers3

1

You can combine both the not commands to one as

for root, dirs, files in os.walk(SEARCHDIR):
    files = [f for f in files if not (f[0] == '.' and any(i in f for i in ["BLACKBOX", ".RTN", ".log"]))] 
Sheldore
  • 37,862
  • 7
  • 57
  • 71
0

Change this:

not ("BLACKBOX" or ".RTN" or ".log") in f

To this:

not any([x in f for x in ["BLACKBOX", ".RTN", ".log"]])
goodvibration
  • 5,980
  • 4
  • 28
  • 61
0

You should try something like this:

filterFn = lambda f: f[0] == '.' or any(s in f for s in ['BLACKBOX','.RTN','.log'])
files = [f for f in files if not filterFn(f)]

My local test shows:

>>> files = [f for f in ('.', 'TEST', 'test.RTN') if not filterFn(f)]
>>> files
['TEST']
F1Rumors
  • 920
  • 9
  • 13