python3与子进程通讯,怎么传输值
app.py:
import subprocess
p = subprocess.Popen("test2.exe", stdin = subprocess.PIPE,stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = False)
p.stdin.write("3\n")
p.stdin.write("4\n")
print(p.stdout.read())
下面是test2.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;
}
[解决办法]
传参就简单了,你把第一个参数写成列表即可
就是"test2.exe" 写成 ["test2.exe", '3', '4']
[解决办法]
认真看报错信息,说不能用str,你改用bytes试试,或者用str的话Popen(..., universal_newlines=True)
p.stdin.write(b"3\n")
p.stdin.write(b"4\n")