subprocess.Popen()不会将输出重定向到文件

时间:2018-05-01 04:48:00

标签: python file subprocess output popen

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

输入:

filename.txt:
    123 
    234
    456

输出:

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

我试图通过Python脚本运行简单的C ++程序。我尝试了subprocess.Popen(),subprocess.call(),subprocess.check_output()。但是他们都没有将命令行输出重定向到文件。

0 个答案:

没有答案