1. 程式人生 > >C#呼叫命令列執行python指令碼,這個辦法可以呼叫python第三方模組和對本地檔案進行操作

C#呼叫命令列執行python指令碼,這個辦法可以呼叫python第三方模組和對本地檔案進行操作

          string pythonScriptPath = Server.MapPath(@"~\pythonScript");//python指令碼所在的目錄
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = "python";//執行python.exe
            //執行python指令碼的命令
            start.Arguments = pythonScriptPath + @"\sen2senScore_v3_console.py "
; //設定執行python指令碼的初始目錄 這裡注意:如果你的python指令碼有檔案操作,必須設定初始目錄 start.WorkingDirectory = pythonScriptPath; start.UseShellExecute = false; start.CreateNoWindow = true; start.RedirectStandardOutput = true; start.RedirectStandardError = true
; using (Process process = Process.Start(start)) { using (StreamReader reader = process.StandardOutput) { string line = reader.ReadLine();//每次讀取一行命令列列印的資訊 } }