1. 程式人生 > >python v3.3.2中使用subprocess模組與其它程式進行管道通訊

python v3.3.2中使用subprocess模組與其它程式進行管道通訊

下面是python指令碼sup.py:

import subprocess

p = subprocess.Popen("test.exe", stdin = subprocess.PIPE,stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines=True,shell = 

False)
p.stdin.write('5\n')
p.stdin.write('3\n')
print(p.stdout.read())

下面是test.exe的C++原始檔:
#include <iostream>
using namespace std;

int main(int argc, const char *artv[])
{
	int x, y;
	cout << "input x:"<< endl;
	cin >> x;
	cout << "input y:"<< endl;
	cin >> y;
	cout << x << " + " << y << " = " << x + y << endl;

	return 0;
}

在windows的控制檯中執行sup.py,結果如下圖所示: