0

I need to call a python script from C#, I'll be doing it like this:

   System.Diagnostics.Process process = new System.Diagnostics.Process();
   System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
   startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
   startInfo.FileName = "cmd.exe";
   startInfo.Arguments = "/C python \"C:\\working\\projects\\python tests\\Sample Data.py\"";
   process.StartInfo = startInfo;
   process.Start();

but I now need to add in command line arguments. I'm trying to decide between just using sys.argv which seems very simple to implement or to go with argparse. If I will always pass one and only one parameter (a date), is there any advantage to using argparse?

Additional info regarding the problem (slightly tangential to the question):

The date I'm passing is a parameter for a SQL query. I could instead run this query in C# (which I would prefer) but then I will need to pass the result to python via command line arguments which seems to me to be a terrible idea but maybe this is something argparse can handle? The table has two date columns and 4 float columns. Can such a table be passed this way?

The reason I am calling python via cmd.exe and not using IronPython is (A) I only need to pass information once from C# to Python so the communication between the two is very limited and (B) the result of which is a 3D surface plot generated by mplot3d which seems like a huge hassle to make work in IronPython (which actually generally confuses me anyway), so if I am just passing the single date then this doesn't seem unreasonable. But if I could pass that entire table easily, either by a command line argument or else some other not overly complicated method, I would be very interested in hearing how.

1 Answer 1

1

Since you're a nice clean slate of knowledgeless bliss, learn argparse. It's piss-easy and replaces sys.arg which is now considered old, archaic, and (probably) deprecated; although you'll find it far more common, for now, because it's been around since Guido was a baby.

Sign up to request clarification or add additional context in comments.

1 Comment

I think I'm actually just going to write the datatable to a CSV and then python can read that instead of trying to pass it as a command line argument which seems like a terrible idea so just going to pass the date. I'll go with argparse like you have suggested just in case I ever have to do it again in the future but it does seem like overkill in this particular situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.