从C#运行python脚本

时间:2016-03-16 12:18:43

标签: c# python python-2.7 process

我想从C#

运行python脚本

并且正在打开shell,但脚本不会运行

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

我该如何运行这个过程?

1 + 2 + 100

4 个答案:

答案 0 :(得分:0)

也许你需要把两个' \'在每个' \'在这一行:

   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  

我希望这对你有帮助:))

答案 1 :(得分:0)

我认为你在这一行的末尾有一个双反斜杠:

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

答案 2 :(得分:0)

您的代码对我来说很合适。由于字符串常量以@开头,因此不应该使用双斜杠。我建议您尝试将exe路径和参数路径复制并粘贴到shell窗口中,然后从shell窗口运行exe以确保在这些路径中没有拼写错误。

答案 3 :(得分:0)

要在字符串中加一个反斜杠,你需要像这样对它进行转义:" \"

所以你的代码将是:

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();

度过愉快的一天,希望我帮助你;)

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

相关问题