Subprocess.Popen:将stdout和stderr克隆到终端和变量
是否可以修改以下代码以从'stdout'和'stderr'打印输出:
印在终端上(实时),
最后存储在out和errs变量中?
代码:
#!/usr/bin/python3# -*- coding: utf-8 -*-import subprocessdef run_cmd(command, cwd=None): p = subprocess.Popen(command, cwd=cwd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) outs, errs = p.communicate() rc = p.returncode outs = outs.decode('utf-8') errs = errs.decode('utf-8') return (rc, (outs, errs))
繁花如伊
相关分类