How to send data to Python script from C #

I experimented with Python with C # and I don't know how to execute a Python script and write It commands / data. I watched this link: " https://code.msdn.microsoft.com/windowsdesktop/C-and-Python-interprocess-171378ee "

But just show how to read the output of a Python script, and not how to write something after opening / executing a script.

For example, I want to open a python script (from C Sharp) and send it some data from C # to the raw_input () function.

Could you help me? Links, examples, all are welcome ... I'm lost now.

PS: I do not want to use ironpython

C # Example:

private void button1_Click(object sender, EventArgs e)
        {
            string python = @"C:\Python27\python.exe";;
            string myPythonApp = "myscript/test01.py"; 

            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);

            myProcessStartInfo.UseShellExecute = false
            myProcessStartInfo.CreateNoWindow = true
            myProcessStartInfo.RedirectStandardOutput = true
            myProcessStartInfo.RedirectStandardInput = true;

            myProcessStartInfo.Arguments = myPythonApp;

            //<>//---------------ISSUE?--------------------------------
            Process myProcessW = new Process();
            Process myProcessR = new Process();//

            myProcessW.StartInfo = myProcessStartInfo;
            myProcessR.StartInfo = myProcessStartInfo;

            myProcessW.Start();
            myProcessR.Start();

            StreamWriter myStreamWriter = myProcessW.StandardInput;
            string inputText;
            inputText = textBox1.Text;
            myStreamWriter.WriteLine(inputText); // <--OK!

            myProcessW.WaitForExit();
            myProcessW.Close();

            // 
            StreamReader myStreamReader = myProcessR.StandardOutput;
            MessageBox.Show("01"); //For Debug, is executed
            string myString = myStreamReader.ReadLine();

            MessageBox.Show("02"); //For Debug, is NOT executed

            label1.Text = myString;

            myProcessR.WaitForExit();
            myProcessR.Close();
            //<>//---------------/ISSUE?--------------------------------
        }

Python example (myscript / test01.py):

 aaa = raw_input("> ")
 print "Ok, you say:",aaa

: , "myProcessStartInfo.RedirectStandardInput = false", "RedirectStandardOutput" , ...

+4
1

, # python ? , # python script , , .

python script, argparse.

0

Source: https://habr.com/ru/post/1652601/


All Articles