zoukankan      html  css  js  c++  java
  • How do I run a Python script from C#?

    How do I run a Python script from C#?

    The reason it isn't working is because you have UseShellExecute = false.

    If you don't use the shell, you will have to supply the complete path to the python executable as FileName, and build the Arguments string to supply both your script and the file you want to read.

    Also note, that you can't RedirectStandardOutput unless UseShellExecute = false.

    I'm not quite sure how the argument string should be formatted for python, but you will need something like this:

    private void run_cmd(string cmd, string args)
    {
         ProcessStartInfo start = new ProcessStartInfo();
         start.FileName = "my/full/path/to/python.exe";
         start.Arguments = string.Format("{0} {1}", cmd, args);
         start.UseShellExecute = false;
         start.RedirectStandardOutput = true;
         using(Process process = Process.Start(start))
         {
             using(StreamReader reader = process.StandardOutput)
             {
                 string result = reader.ReadToEnd();
                 Console.Write(result);
             }
         }
    }
  • 相关阅读:
    poj1328
    xml入门简介--两天学会xml
    php的一些特殊用法
    数据结构(一)
    队列的 基本操作
    栈的 基本操作
    线性表----单链表
    线性表----顺序表
    数据结构
    链式队列
  • 原文地址:https://www.cnblogs.com/chucklu/p/14743293.html
Copyright © 2011-2022 走看看