Python 使用文件作为子进程的标准输入和标准输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15167603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:30:11 来源:igfitidea点击:
Using files as stdin and stdout for subprocess
提问by Nolander
How do I replicate the following batch command using python subprocess module?
如何使用 python subprocess 模块复制以下批处理命令?
myprogram < myinput.in > myoutput.out
In other words, how do I run myprogramusing the contents of myinput.inas the standard input and myoutput.outas standard output?
换句话说,我如何myprogram使用 的内容myinput.in作为标准输入和myoutput.out标准输出运行?
回答by Elmar Peise
The following should work:
以下应该工作:
myinput = open('myinput.in')
myoutput = open('myoutput.out', 'w')
p = subprocess.Popen('myprogram.exe', stdin=myinput, stdout=myoutput)
p.wait()
myoutput.flush()

