I am trying to run a command using subprocess in python and trying to read the output of it and copy it into a file. my code is:
command = "%s -sK:\\run_one_test.csh %s %s" % (PATH, file, VERSION)
p = subprocess.Popen(command,stdout=subprocess.PIPE)
text = p.communicate()[0]
return_code = p.returncode
with open("%s/%s%s" % (LOG_DIR, file, LOG_EXT), "w") as f:
f.writelines([l.decode for l in text.split('\n')])
f.close()
But when i use splitlines i get the error message:
f.writelines([l.decode for l in text.split('\n')])
TypeError: a bytes-like object is required, not 'str'
Why does it happen? I used decode. Also, is this the right way to split the lines of code using "\n"? Thanks.