Some Python commands do not fall into Stdout

I wrote a simple program that captures and runs Python command line scripts, but there is a problem. Text passed to the Python input function is not written to my program, even though my program writes stdout.

For example: Python script:

import sys print("Hello, World!") x = input("Please enter a number: ") print(x) print("This work?") 

Would write "Hello, World!" then stop. When I give him the number, he will continue to write "Please enter the number: 3". What's happening? Any solutions? My C # is as follows:

 public partial class PyCon : Window { public string strPythonPath; public string strFile; public string strArguments; private StreamWriter sw; public PyCon(string pythonpath, string file, string args) { strPythonPath = pythonpath; strFile = file; strArguments = args; InitializeComponent(); Process p = new Process(); p.StartInfo.FileName = strPythonPath; p.StartInfo.Arguments = "\"" + strFile + "\" " + strArguments; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); p.Start(); p.BeginOutputReadLine(); p.BeginErrorReadLine(); sw = p.StandardInput; } private void p_OutputDataReceived(object sendingProcess, DataReceivedEventArgs received) { if (!String.IsNullOrEmpty(received.Data)) { AppendConsole(received.Data); } } private void p_ErrorDataReceived(object sendingProcess, DataReceivedEventArgs received) { if (!String.IsNullOrEmpty(received.Data)) { AppendConsole(received.Data); } } private void AppendConsole(string message) { if (!txtConsole.Dispatcher.CheckAccess()) { txtConsole.Dispatcher.Invoke(DispatcherPriority.Normal, (System.Windows.Forms.MethodInvoker)delegate() { txtConsole.AppendText(message + "\n"); }); } else { //Format text message = message.Replace("\n", Environment.NewLine); txtConsole.AppendText(message + "\n"); } } private void txtInput_KeyUp(object sender, KeyEventArgs e) { if (e.Key != Key.Enter) return; sw.WriteLine(txtInput.Text); txtInput.Text = ""; } } 

Edit: After a lot of research and help from this thread, I came to the conclusion that the problem is that the Python input command does not call C # DataReceivedEventHandler. Perhaps this is not a solution to this other than changing scripts. If so, I will make an answer containing these changes as accepted. Thanks for the help guys!

+1
python c # input stdout
source share
2 answers

Smells such as Python I / O are buffered in a line, i.e. wait for CRLF to send an entire line at once. You can try disabling this (python -u myscript.py or setting the environment variable PYTHONUNBUFFERED) or bypassing it like this:

 print("Hello, World!") print("Please enter a number: ") x = input() print(x) 
+1
source share

It's hard to say because I'm using python 2.6, and you seem to be using 3.x, and I haven't been programmed in C # for too long, but I assume this line:

 sw.WriteLine(txtInput.Text); 

Sends a "3" plus a new Windows string character.

Try:

 sw.Write(txtInput.Text + "\n") sw.Flush() 

This will just send a normal newline instead of returning a Windows carriage, which could be a problem. Make sure you always execute Flush when dealing with complex streaming like this!

One more thing, make sure you can simply redirect this to the command line. Too often, programmers try to do everything at once and get stuck:

 ./stdintest.py < input.txt 

If it does not work, it will not work in C #. Good luck.

+1
source share

All Articles