将文件用作stdin和stdout用于子进程

时间:2021-09-01 00:05:45

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?

2 个解决方案

#1


27  

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

#2


-5  

opens a file in subprocess. It is a blocking call. So control will not go to next line till, file is open.

在子进程中打开一个文件。这是一个阻止电话。所以控制不会到下一行,直到文件打开。

ss=subprocess.Popen(FileName,shell=True)
ss.communicate()

#1


27  

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

#2


-5  

opens a file in subprocess. It is a blocking call. So control will not go to next line till, file is open.

在子进程中打开一个文件。这是一个阻止电话。所以控制不会到下一行,直到文件打开。

ss=subprocess.Popen(FileName,shell=True)
ss.communicate()