subprocess.Popen() doesn't redirect output to file

妖精的绣舞 提交于 2019-12-24 06:10:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!