【发布时间】:2018-09-04 18:51:58
【问题描述】:
我正在尝试使用 python 中的子进程运行命令并尝试读取它的输出并将其复制到文件中。 我的代码是:
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()
但是当我使用分割线时,我收到错误消息:
f.writelines([l.decode for l in text.split('\n')])
TypeError: a bytes-like object is required, not 'str'
为什么会这样?我用解码。 另外,这是使用“\n”分割代码行的正确方法吗? 谢谢。
【问题讨论】:
标签: python