in py3k the output of subprocess.PIPE is bytes

must be converted to string with .decode("utf-8") before writing to the
mutated output file