【发布时间】:2018-07-24 03:59:40
【问题描述】:
我想写一个函数,一次执行多个shell命令,并实时打印shell返回的内容。
我目前有以下不打印 shell 的代码(我使用的是 Windows 10 和 python 3.6.2):
commands = ["foo", "foofoo"]
p = subprocess.Popen("cmd.exe", shell=True, stdin=subprocess.PIPE, \
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for command in commands:
p.stdin.write((command + "\n").encode("utf-8"))
p.stdin.close()
p.stdout.read()
如何实时查看 shell 返回的内容?
编辑:这个问题不是 cmets 中前两个链接的重复,它们无助于实时打印。
【问题讨论】:
标签: python shell subprocess