使用C#调用Python脚本,带参数列表 z

static void Main(string[] args)

        {

            string[] strArr;//参数列表

         string sArguments = @"Pythons.py";//这里是python的文件名字

         RunPythonScript(sArguments, "-u", strArr);

    

            }



public static void RunPythonScript(string sArgName, string args = "",params string[] teps)

        {

            Process p = new Process();

            string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + sArgName;// 获得python文件的绝对路径     

            p.StartInfo.FileName = @"python";

            string sArguments = path;

            if (tep.Length > 0)

            {

                foreach (string sigstr in teps)

                {

                    sArguments += " " + sigstr;//传递参数

                }

            }

            if (args.Length > 0)

            {



                sArguments += " " + args;



            }



            p.StartInfo.Arguments = sArguments;



            p.StartInfo.UseShellExecute = false;



            p.StartInfo.RedirectStandardOutput = true;



            p.StartInfo.RedirectStandardInput = true;



            p.StartInfo.RedirectStandardError = true;



            p.StartInfo.CreateNoWindow = true;



            p.Start();

            p.BeginOutputReadLine();

            p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

            Console.ReadLine();

            p.WaitForExit();

        }

        //输出打印的信息

        static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)

        {

            if (!string.IsNullOrEmpty(e.Data))

            {

                 AppendText(e.Data + Environment.NewLine);

            }

        }

        public delegate void AppendTextCallback(string text);

        public static void AppendText(string text)

        {

            Console.WriteLine(text);

           

        }

传递参数时,每个参数中间要有一个空格

 

Python接收参数的方法:
从1开始接收参数

args1= sys.argv[1]

args2=sys.argv[2]

args2=sys.argv[3]

你可能感兴趣的:(python)