the program begins by setting the number string to the given number or to an empty string if we are not debugging since the program is running as child process and the subprocess module only reads and writes binary data and always uses the local encodingwe must read sys stdin' underlying buffer of binary data and perform the decoding ourselves once we have read the binary datawe decode it into unicode string and split it into lines the child process then reads the first linesince this contains the search word here are the lines that are different for python sys stdin sys stdin detach(stdin sys stdin read(lines stdin decode("utf ""ignore"splitlines(python provides the sys stdin detach(method that returns binary file object we then read in all the datadecode it into unicode using the encoding of our choiceand then split the unicode string into lines for filename in lines[ :]filename filename rstrip(previous "trywith open(filename"rb"as fhwhile truecurrent fh read(block_sizeif not currentbreak current current decode("utf ""ignore"if (word in current or word in previous[-len(word):current[:len(word)])print("{ }{ }format(numberfilename)break if len(current!block_sizebreak previous current except environmenterror as errprint("{ }{ }format(numbererr)all the lines after the first are filenames (with pathsfor each one we open the relevant fileread itand print its name if it contains the search word it is possible that some of the files might be very large and this could be problemespecially if there are child processes running concurrentlyall reading big it is possible that future version of python will have version of the subprocess module that allows encoding and errors arguments so that we can use our preferred encoding without having to access sys stdin in binary mode and do the decoding ourselves see bugs python org/issue