このトピックについて多くの質問があったことは理解できますが、どれも私の問題を実際に解決することはできませんでした. ここに私のコードを示しました。ここで間違いを指摘してもらいたいと思います。
Python実行可能ファイル/ファイルを呼び出すC#で書かれたプログラムがあります。最初の要件は、入力ストリームを介して 1 つの引数を Python ファイルに渡すことです。これは私がすることができました。私が今直面している本当の問題は、私のpythonファイルが「引数_xを入力してください」と出力しているかどうかを確認する必要があることです.C#コードでこの出力を読み、それがargument_xであるかどうかを確認し、次に引数値のみを書き込む必要があります入力ストリーム。以下は、C# と Python のコード スニペットです。
C# コードは次のとおりです。
using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//Create a new process object
Process myProcess = new Process();
//Provide the start information for the process
myProcess.StartInfo.FileName = "python.exe";
myProcess.StartInfo.Arguments = "mytestpython.py";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
StreamReader myStreamReader;
StreamWriter myStreamWriter;
//Invoke the process from current process
myProcess.Start();
myStreamReader = myProcess.StandardOutput;
//Read the standard output of the spawned process.
string myString = myProcess.StandardOutput.ReadToEnd();
Console.WriteLine(myString);
if (myString.Contains("argument_x"))
{
myStreamWriter = myProcess.StandardInput;
String argument = "argument_value";
myStreamWriter.WriteLine(argument);
}
myProcess.WaitForExit();
myStreamWriter.Close();
myStreamReader.Close();
myProcess.Close();
}
}
}
mytestpython.pyファイル内の python プログラムは次のようになります。
import sys
import getpass
prompt_string = "Please enter argument_x"
if sys.stdin.isatty():
reqd_arg = getpass.getpass(prompt=prompt_string)
else:
print(prompt_string)
reqd_arg = sys.stdin.readline().rstrip()
コードの 90% は正しく書かれていると感じているので、助けてください。よろしくお願いします。