简体   繁体   English

使用文件作为子流程的stdin和stdout

[英]Using files as stdin and stdout for subprocess

How do I replicate the following batch command using python subprocess module? 如何使用python子进程模块复制以下批处理命令?

myprogram < myinput.in > myoutput.out

In other words, how do I run myprogram using the contents of myinput.in as the standard input and myoutput.out as standard output? 换句话说,如何使用myinput.in的内容作为标准输入并使用myoutput.out的标准输出来运行myprogram

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM