我想从C#中执行一些Python代码,因此我有如下代码:
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"C:\Users\protieax\PycharmProjects\untitled\venv\Scripts\python.exe";
start.Arguments = "request.py 31.12.2016 01.01.2017 datatype"; // give filename, dates from the UI to python and query datatype
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
它应该可以工作,但是C#找不到Python文件。
我应该在这两个变量中放什么?谢谢。
编辑:
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"U:\Documents\entsoe-py-master\tests\test_data";
start.Arguments = "request.py 31.12.2016 01.01.2017 datatype"; // give filename, dates from the UI to python and query datatype
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
这是我的代码,经过您的修改。我遇到了一个新错误:
System.ComponentModel.Win32Exception: 'The system cannot find the file specified'
在这一行,
using (Process process = Process.Stqrt(start))
C:\Users\protieax\PycharmProjects\untitled\venv\Scripts\request.py
。 - Franck