将_io.BufferedReader转换为str以进行write()

时间:2018-09-03 13:44:25

标签: python subprocess stdout popen

如何获取我的write()函数来接受我的stdout = subprocess.PIPE输出? 我不断遇到类型错误问题。

这是我的代码:

with open("logger.txt","w") as fobj:                                         
    baby = subprocess.Popen('bash arguments', stdout = subprocess.PIPE,  shell = True)
    fobj.write(baby.communicate())

我输入错误

我尝试了这个,没有错误,但文件始终为空

with open("logger.txt","w") as fobj:                                         
    baby = subprocess.Popen('bash arguments', stdout = subprocess.PIPE,  shell = True)
    baby.communicate()

有人可以帮助我吗?谢谢

1 个答案:

答案 0 :(得分:2)

如果您只想将脚本的输出写入fobj

with open("logger.txt","wb") as fobj:                                         
    subprocess.check_call('bash arguments', stdout=fobj, shell=True)