考虑这段代码,其中subprocess.Popen
被催生。我想写入子进程'stdout
and 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()
没有使用方法,指定a有什么用stdout=
在这种情况下参数?
根据文档:
stdin、stdout 和 stderr 分别指定执行程序的标准输入、标准输出和标准错误文件句柄。有效值为PIPE、DEVNULL、现有文件描述符(正整数)、现有文件对象和 None.
Using subprocess.PIPE:
proc = subprocess.Popen(['bash', '-c', 'echo Testing'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
print('Writing:', proc.stdout.read())
# OR print('Writing:', proc.stdout.read().decode())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)