我有GUI控制器,用C#WPF编写,用args运行python脚本。在这个python脚本中有一些打印和写入文件。我将stdout和stderr输出重定向到TextBlock。但是这些产出中没有一个不起作用。任何想法?
C#runner:
...
pr.StartInfo.FileName = pythonPath;
pr.StartInfo.Arguments = scriptPath + " " + args;
pr.StartInfo.UseShellExecute = false;
pr.StartInfo.RedirectStandardOutput = true;
pr.StartInfo.RedirectStandardError = true;
pr.StartInfo.CreateNoWindow = true;
pr.ErrorDataReceived += new DataReceivedEventHandler(sortErrorHandler);
pr.OutputDataReceived += new DataReceivedEventHandler(sortOutputHandler);
pr.EnableRaisingEvents = true;
//pr.Exited += new EventHandler(whenExitProcess);
TB.Text = "";
pr.Start();
pr.BeginOutputReadLine();
pr.BeginErrorReadLine();
}
e.Handled = true;
}
private void sortErrorHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
Dispatcher.BeginInvoke(new MethodInvoker(delegate
{
if (!String.IsNullOrEmpty(outLine.Data))
{
TB.Text += outLine.Data + Environment.NewLine;
}
}));
}
private void sortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
Dispatcher.BeginInvoke(new MethodInvoker(delegate
{
if (!String.IsNullOrEmpty(outLine.Data))
{
TB.Text += outLine.Data + Environment.NewLine;
}
}));
}
的Python:
print "Hello"
file.open("test", "a")
file.write("Hello again")
file.close()
... remaining code
好的,sys.stderr.write(“test”)正在运行,但是stdout和stderr之间可能有什么区别?
答案 0 :(得分:0)
如果您没有收到任何数据,可能需要在python脚本中调用sys.stdout.flush()
(和/或sys.stderr.flush()
)。
我不清楚你是否说写文件也有问题。