问题
Check.cpp
int main(int argc, char** argv)
{
string file = "";
ifstream inFile;
int x;
file = argv[1];
inFile.open(file.c_str());
while (inFile >> x)
cout << x << endl;
return 0;
}
Print.py
from __future__ import with_statement
from shutil import copy
import subprocess
import filecmp
import sys
count = 0
f = open ("filename.txt","r")
file1 = open ("file1.txt","w")
file2 = open ("file2.txt","w")
file3 = open("out1.txt","w")
file4 = open("out2.txt","w")
for line in range(1,2):
firstline = f.readline()
file1.write(firstline)
subprocess.Popen(["./a.out","file1.txt"],stdout=file3)
file2.write(firstline)
subprocess.Popen(["./a.out","file2.txt"],stdout=file4)
if (filecmp.cmp('out1.txt', 'out2.txt') == True):
count = count + 1
print count
Input:
filename.txt:
123
234
456
Output:
file1.txt:
123
file2.txt:
123
out1.txt:
Expected Output - 123. But no output is returned
out2.txt
Expected Output - 123. But no output is returned
I am trying to run the simple C++ program through Python script. I tried subprocess.Popen(), subprocess.call(), subprocess.check_output(). But none of them redirecting the command line output to file.
来源:https://stackoverflow.com/questions/50111465/subprocess-popen-doesnt-redirect-output-to-file