我有 GUI 控制器,用 C# WPF 编写,它运行带有 args 的 python 脚本。在这个 python 脚本中有一些打印和写入文件。我将 stdout 和 stderr 输出重定向到 TextBlock。但是没有一个输出不起作用。有任何想法吗?
C# 跑步者:
...
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 之间可能有什么区别?