【发布时间】:2016-08-11 21:24:48
【问题描述】:
我现在有这个代码
p = subprocess.Popen(args='test.bat'.split(), stdout=sp.PIPE, stdin=sp.PIPE, universal_newlines=True)
p.stdin.write("n")
p.stdin.flush()
for line in p.stdout:
print line
所以当批处理文件运行时它会询问
Are you sure you want to continue[Y/N]?
你输入n,它再次弹出,接下来n 关闭它。但标准输入只输入第一个n 而不是第二个。如果我只将它与一个n 一起使用,则代码完成并完成,但我如何给子进程 2 个输入?
另一件事:
我想读取输出,这样我就可以运行这样的东西:
p = subprocess.Popen(args='test.bat'.split(), stdout=sp.PIPE, stdin=sp.PIPE, universal_newlines=True)
for line in p.stdout:
if line.startswith('Please type x/y"):
p.stdin.write('x')
p.stdin.flush()
if line.startswith('please choose 1/2'):
p.stdin.write('2')
p.stdin.flush()
现在运行不会进入for,因为子进程没有完成。
第二件事,取第一件事并使其复杂化。所以回答第二个就足够了。
谢谢你
【问题讨论】:
标签: python-2.7 input subprocess stdin