Search code examples
c#pythonpython-2.7command-line-argumentsprocessstartinfo

How to pass string as command line argument in python using C#


I am try to send the string as command line argument to my python script as follow:

var cmdToBeExecute = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\myScript.py";
            var start = new ProcessStartInfo
            {
                FileName = "C:\\Python27\\python.exe",
                Arguments = string.Format("{0} {1}", cmdToBeExecute, "Rahul done good"),
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            };

and i am try to read the command line argument and display it in python as:

import sys
print sys.argv[1]

But, I get only the first word "Rahul" as output. Please help me to send string as command parameter argument to my python script.


Solution

  • You should pass your string inside double quotes if it contains spaces:

    Arguments = string.Format("{0} {1}", cmdToBeExecute, "\"Rahul done good\""),
    

    This is not specific to C#; you should use quotes even if you run it from the command line:

    python myscript.py "Rahul done good"