【发布时间】:2014-10-13 22:45:23
【问题描述】:
我正在使用子进程来运行可执行文件并使用通信管道输出它的输出。最后,我将通信的内容写入文件。具体代码如下所示
run = subprocess.Popen(['executable'], stdout=subprocess.PIPE)
output = run.communicate()[0]
logfile = open('run.log', 'a')
logfile.write(output)
logfile.close()
在上述过程中,日志文件是在运行结束时写入的。但是,有没有办法在可执行文件运行时将输出写入日志?
【问题讨论】:
-
这在the docs中直接解释:“stdin,stdout和stderr指定执行程序的标准输入、标准输出和标准错误文件句柄。有效值为
PIPE、DEVNULL、现有文件描述符(正整数)、现有文件对象和None。"
标签: python subprocess stdout communicate