考虑下面的代码,其中subprocess.Popen产生了a。我想写入子流程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()
Run Code Online (Sandbox Code Playgroud)
为什么.write()不使用该方法stdout=?在这种情况下,指定参数有什么用?
根据文档:
\n\n\n\n\n\n\nstdin、stdout 和 stderr 分别指定执行的程序\xe2\x80\x99 的标准输入、标准输出和标准错误文件句柄。有效值为PIPE、DEVNULL、现有文件描述符(正整数)、现有文件对象和 None。
\n
proc = subprocess.Popen([\'bash\', \'-c\', \'echo Testing\'], \n stdout=subprocess.PIPE,\n stderr=subprocess.STDOUT)\nprint(\'Writing:\', proc.stdout.read())\n# OR print(\'Writing:\', proc.stdout.read().decode())\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2212 次 |
| 最近记录: |