import os for root, dirs, files in os.walk("."): if "test" in root: continue # Tests are generally legitimate and uninteresting. for fn in files: fn = os.path.join(root,fn) if not fn.endswith(".py"): continue with open(fn,"rb") as f: for l,line in enumerate(f,1): try: line.decode("ascii") continue # Ignore the ASCII lines except UnicodeDecodeError: line = line.rstrip(b"\n") try: line = line.decode("UTF-8") except UnicodeDecodeError: line = repr(line) # If it's not UTF-8 either, show it as b'...' print("%s:%d: %s" % (fn,l,line))