I have a python script 'saudilp.py' (it analyze car plate number) and i try to run this script inside c#
this c# code:
// 1) create process info
ProcessStartInfo start = new ProcessStartInfo();
//cmd is full path to python.exe
start.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\python.exe";
// 2) Provide script and arguments
string arg = "s10.jpeg";
string pathScript = "D:/graduation project/pythonOCRtest/pythonOCRtest/OCR/saudilp.py";
start.Arguments = $"\"{pathScript}\"\"{arg}\""; //args is path to .py file and any cmd line args
// 3) process configuration
start.UseShellExecute = false;
start.CreateNoWindow = true; //do not create window
start.RedirectStandardOutput = true; //recive print lines from the script
start.RedirectStandardError = true;
// 4) Execute process and get output
string result = "";
string errors = "";
using (Process process = Process.Start(start))
{
errors = process.StandardError.ReadToEnd();
result = process.StandardOutput.ReadToEnd();
}
// 5) display output
Console.WriteLine("ERRORS:");
Console.WriteLine(errors);
Console.WriteLine();
Console.WriteLine("RESULTS:");
Console.WriteLine(result);
The console print this error:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\python.exe: can't open file 'D:\graduation project\pythonOCRtest\pythonOCRtest\OCR\saudilp.py"s10.jpeg': [Errno 22] Invalid argument
CodePudding user response:
You seams to have an extra \ on your arguments, try
start.Arguments = $"\"{pathScript}\" {arg}\"";
also you may need to set-up the working directory
start.WorkingDirectory = PATH