如何从 C# 中找出 python 脚本 运行 通过或失败
How to figure out the python script ran from C# has passed or failed
我有以下从 C# 调用 Python 应用程序的代码,有没有办法确定 C# 中的 python 脚本 运行 是通过还是失败?我们是否从 C#
获得任何 exit/return 指示 python 应用程序 运行 成功或失败的代码
using System;
using System.IO;
using System.Diagnostics;
namespace CallPython
{
class Program
{
static void Main(string[] args)
{
string python = @"C:\Python27\python.exe";
string myPythonApp = @"C:\Dropbox\scripts\loadbuild\sum.py";
int x = 2;
int y = 5;
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.Arguments = myPythonApp + " " + x + " " + y;
Process myProcess = new Process();
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadToEnd();
myProcess.WaitForExit();
myProcess.Close();
Console.WriteLine("Value received from script: " + myString);
Console.ReadLine();
}
}
}
除了程序写入其(标准)输出通道的输出外,程序总是以 exit status(或退出代码)终止。如果程序正确 运行,则程序应以退出代码 0
退出,如果出错,则退出代码应为 0
以外的退出代码。
现在 python 程序通常会在出现算术错误时以 0
以外的退出状态退出,例如程序:
a = 4/0
不仅会产生错误信息:
Traceback (most recent call last):
File "test.ply", line 1, in <module>
a = 4/0
ZeroDivisionError: integer division or modulo by zero
还有 return 退出状态 1
。现在显然 python 程序员可以使用 try
-catch
处理,这样错误就不会导致非零的退出状态,但这会 - 至少部分地 - 阻止使用退出状态。
如果 python
程序的程序员在错误正确时实现了退出代码,或者错误是算术错误,则 python
程序将以非零的退出代码退出.
您可以使用 Process.ExitCode
捕获退出代码。
在文档手册中,我们可以看到:
Developers usually indicate a successful exit by an ExitCode
value of zero, and designate errors by nonzero values that the calling method can use to identify the cause of an abnormal process termination. It is not necessary to follow these guidelines, but they are the convention.
例如:
//...
myProcess.WaitForExit();
int ec = myProcess.ExitCode;
if(ec != 0) {
//oops the Python program made an error
} else {
//assume everything went fine
}
我有以下从 C# 调用 Python 应用程序的代码,有没有办法确定 C# 中的 python 脚本 运行 是通过还是失败?我们是否从 C#
获得任何 exit/return 指示 python 应用程序 运行 成功或失败的代码using System;
using System.IO;
using System.Diagnostics;
namespace CallPython
{
class Program
{
static void Main(string[] args)
{
string python = @"C:\Python27\python.exe";
string myPythonApp = @"C:\Dropbox\scripts\loadbuild\sum.py";
int x = 2;
int y = 5;
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.Arguments = myPythonApp + " " + x + " " + y;
Process myProcess = new Process();
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadToEnd();
myProcess.WaitForExit();
myProcess.Close();
Console.WriteLine("Value received from script: " + myString);
Console.ReadLine();
}
}
}
除了程序写入其(标准)输出通道的输出外,程序总是以 exit status(或退出代码)终止。如果程序正确 运行,则程序应以退出代码 0
退出,如果出错,则退出代码应为 0
以外的退出代码。
现在 python 程序通常会在出现算术错误时以 0
以外的退出状态退出,例如程序:
a = 4/0
不仅会产生错误信息:
Traceback (most recent call last):
File "test.ply", line 1, in <module>
a = 4/0
ZeroDivisionError: integer division or modulo by zero
还有 return 退出状态 1
。现在显然 python 程序员可以使用 try
-catch
处理,这样错误就不会导致非零的退出状态,但这会 - 至少部分地 - 阻止使用退出状态。
如果 python
程序的程序员在错误正确时实现了退出代码,或者错误是算术错误,则 python
程序将以非零的退出代码退出.
您可以使用 Process.ExitCode
捕获退出代码。
在文档手册中,我们可以看到:
Developers usually indicate a successful exit by an
ExitCode
value of zero, and designate errors by nonzero values that the calling method can use to identify the cause of an abnormal process termination. It is not necessary to follow these guidelines, but they are the convention.
例如:
//...
myProcess.WaitForExit();
int ec = myProcess.ExitCode;
if(ec != 0) {
//oops the Python program made an error
} else {
//assume everything went fine
}