import os, os.path path = 'rts/' excludes = ['rts/lib','rts/build'] ends = ['.cpp'] for root, dirs, files in os.walk(path): if any(root.startswith(x) for x in excludes): continue print '***',root for fname in (fn for fn in files if any(fn.endswith(x) for x in ends)): f = open(os.path.join(root, fname)) if any('mmgr.h' in line for line in f): continue else: print fname