简体   繁体   中英

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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