import subprocess for fn in subprocess.run(["hg","files",".","-0"], stdout=subprocess.PIPE).stdout.decode("ascii").split("\0"): if "test" in fn: continue if b"text" not in subprocess.run(["file",fn],stdout=subprocess.PIPE).stdout: 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))