简体   繁体   English

从C#运行python脚本

[英]Running python script from C#

I am tring to run python script from C# 我正在尝试从C#运行python脚本

and from the shell is being opened but, the script don't run 并且从外壳程序被打开,但是脚本不运行

I know it since it should create a file 我知道,因为它应该创建一个文件

How can I run the process? 如何运行该过程?

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

Maybe you need to put two '\\' in each '\\' in this line: 也许您需要在此行的每个“ \\”中放置两个“ \\”:

   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  

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

Edit: 编辑:

Try like this: 尝试这样:

   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  

I hope this will helpful to you :) 希望对您有帮助:)

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

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

Your code looks right to me. 您的代码对我来说似乎正确。 The double slash should not be necessary since the string constant is preceded with @. 由于字符串常量前面带有@,因此不需要双斜杠。 What I recommend you try is copy and paste the exe path and the argument path into a shell window, then run the exe from the shell window to be sure that you don't have a typo in those paths. 我建议您尝试的操作是将exe路径和参数路径复制并粘贴到Shell窗口中,然后从Shell窗口运行exe,以确保这些路径中没有错字。

To put a backslash in your string you need to escape it like this : "\\" 要在字符串中加上反斜杠,您需要像这样将其转义:“ \\”

So your code will be : 因此您的代码将是:

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

Have a nice day and hope i helped you ;) 祝你有美好的一天,希望我能对你有所帮助;)

edit: Apparently the double backslash is not needed when there is an @ before the string. 编辑:显然,当字符串前有@时,不需要双反斜杠。 Try to test your location directly with your operating system. 尝试直接使用操作系统测试您的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM