你可以在命令输出行之后立即使用
iter处理行
lines = iter(fd.readline, "")。这是显示典型用例的完整示例:
from __future__ import print_function # only Python 2.ximport subprocessdef execute(cmd): popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True) for stdout_line in iter(popen.stdout.readline, ""): yield stdout_line popen.stdout.close() return_pre = popen.wait() if return_pre: raise subprocess.CalledProcessError(return_pre, cmd)# Examplefor path in execute(["locate", "a"]): print(path, end="")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)