使用文件作为子进程的标准输入和标准输出 [英] Using files as stdin and stdout for subprocess

查看:34
本文介绍了使用文件作为子进程的标准输入和标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 python subprocess 模块复制以下批处理命令?

How do I replicate the following batch command using python subprocess module?

myprogram < myinput.in > myoutput.out

换句话说,我如何使用 myinput.in 的内容作为标准输入和 myoutput.out 作为标准来运行 myprogram输出?

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 = open('myinput.in')
myoutput = open('myoutput.out', 'w')
p = subprocess.Popen('myprogram.exe', stdin=myinput, stdout=myoutput)
p.wait()
myoutput.flush()

这篇关于使用文件作为子进程的标准输入和标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆