【问题标题】:Running python script from C#从 C# 运行 python 脚本
【发布时间】:2016-03-16 12:18:43
【问题描述】:

我正在尝试从 C# 运行 python 脚本

从外壳打开,但脚本不运行

我知道,因为它应该创建一个文件

我怎样才能运行这个过程?

Process p = new Process(); // create process (i.e., the python program
p.StartInfo.FileName = @"C:\Python27\python.exe";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false; // make sure we can read the output from stdout
p.StartInfo.Arguments = @"T:\barakr\360_3G_daily_report\2016.03.15\0615319253\powerlink_logs_mrg.py"; //PanelsDirectory[j] + "\\powerlink_logs_mrg.py"; // start the python program with two parameters                        
p.Start(); 

【问题讨论】:

    标签: c# python python-2.7 process


    【解决方案1】:

    要在字符串中添加反斜杠,您需要像这样对其进行转义:“\”

    所以你的代码将是:

    Process p = new Process(); // create process (i.e., the python program
    p.StartInfo.FileName = @"C:\\Python27\\python.exe";
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.UseShellExecute = false; // make sure we can read the output from stdout
    p.StartInfo.Arguments = @"T:\\barakr\\360_3G_daily_report\\2016.03.15\\0615319253\\powerlink_logs_mrg.py"; //PanelsDirectory[j] + "\\powerlink_logs_mrg.py"; // start the python program with two parameters                        
    p.Start();
    

    祝你有美好的一天,希望我能帮到你;)

    edit:当字符串前有@ 时,显然不需要双反斜杠。尝试使用您的操作系统直接测试您的位置。

    【讨论】:

      【解决方案2】:

      也许你需要在这一行的每个'\'中放两个'\':

         p.StartInfo.Arguments = @"T:\barakr\360_3G_daily_report\2016.03.15\0615319253\\powerlink_logs_mrg.py"; //PanelsDirectory[j] + "\\powerlink_logs_mrg.py"; // start the python program with two parameters  
      

      查看此网页:https://bytes.com/topic/python/insights/950783-two-ways-run-python-programs-c

      编辑:

      试试这样:

         p.StartInfo.Arguments = @"T:\\barakr\\360_3G_daily_report\\2016.03.15\\0615319253\\powerlink_logs_mrg.py"; //PanelsDirectory[j] + "\\powerlink_logs_mrg.py"; // start the python program with two parameters  
      

      希望对你有帮助:)

      【讨论】:

      • 如果你在字符串之前使用'@',似乎不需要放一个双'/',对不起。
      【解决方案3】:

      我认为你有一个双反斜杠,应该在这一行的末尾是一个单反斜杠:

      p.StartInfo.Arguments = @"T:\barakr\360_3G_daily_report\2016.03.15\0615319253\\powerlink_logs_mrg.py";
      

      【讨论】:

        【解决方案4】:

        您的代码在我看来是正确的。双斜杠不是必需的,因为字符串常量以 @ 开头。我建议您尝试将 exe 路径和参数路径复制并粘贴到 shell 窗口中,然后从 shell 窗口运行 exe 以确保这些路径中没有拼写错误。

        【讨论】:

        • 它应该是 1 个 slesh。更改。结果相同。
        猜你喜欢
        • 2016-02-05
        • 1970-01-01
        • 2014-11-04
        • 2017-05-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多