【发布时间】:2014-01-28 21:12:36
【问题描述】:
考虑这段代码,其中产生了subprocess.Popen。我想写入子进程的stdout 和stderr 以转到我的自定义文件对象的.write() 方法,但事实并非如此。
import subprocess
class Printer:
def __init__(self):
pass
def write(self, chunk):
print('Writing:', chunk)
def fileno(self):
return 0
def close(self):
return
proc = subprocess.Popen(['bash', '-c', 'echo Testing'],
stdout=Printer(),
stderr=subprocess.STDOUT)
proc.wait()
为什么不使用.write()方法,这种情况下指定stdout=参数有什么用?
【问题讨论】:
-
这是一个使用自定义类文件对象作为子进程 stdout/stderr 的示例:Python subprocess get children's output to file and terminal?
标签: python subprocess