import ast, os n=0 libdir = 'C:/programs/Python34/Lib' for root, dirs, files in os.walk(libdir): print(root) if root.endswith('site-packages'): dirs.clear() continue os.chdir(root) for fname in files: if fname.endswith('.py'): with open(fname, encoding='utf-8') as f: try: tree = ast.parse(f.read()) except (UnicodeDecodeError, SyntaxError): continue try: compile(tree, fname, 'exec') n += 1 except Exception as e: if not fname.startswith('badsyntax'): print(root, fname, type(e).__name__, e) print(n)