Created
November 15, 2015 14:26
Passing arguements from C# to python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
print int(sys.argv[1]) + int(sys.argv[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace CallPython | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ProcessStartInfo start = new ProcessStartInfo(); | |
start.FileName = "C:\\Python27\\python.exe"; | |
Console.Write(args.Length); | |
// arg[0] = Path to your python script (example : "C:\\add_them.py") | |
// arg[1] = first arguement taken from C#'s main method's args variable (here i'm passing a number : 5) | |
// arg[2] = second arguement taken from C#'s main method's args variable ( here i'm passing a number : 6) | |
// pass these to your Arguements property of your ProcessStartInfo instance | |
start.Arguments = string.Format("{0} {1} {2}",args[0],args[1],args[2]); | |
start.UseShellExecute = false; | |
start.RedirectStandardOutput = true; | |
using (Process process = Process.Start(start)) | |
{ | |
using (StreamReader reader = process.StandardOutput) | |
{ | |
string result = reader.ReadToEnd(); | |
// this prints 11 | |
Console.Write(result); | |
} | |
} | |
Console.Read(); | |
} | |
} | |
} |
Thanks for your sample code.
Great
thanks bro
Really helpful for me thx
i have this issue: "The specified executable is not a valid application for this OS platform". Someone an idea what to do?
I fucking love u
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was particularly helpful for me while I was working on AWS S3 when we are using Boto3 with C#. Keep up the good work.