简体   繁体   English

如何使用C#在Python中将字符串作为命令行参数传递

[英]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: 我尝试将字符串作为命令行参数发送到我的python脚本,如下所示:

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: 我试图读取命令行参数,并在python中将其显示为:

import sys
print sys.argv[1]

But, I get only the first word "Rahul" as output. 但是,我只得到第一个单词“ Rahul”作为输出。 Please help me to send string as command parameter argument to my python script. 请帮助我将字符串作为命令参数参数发送到我的python脚本。

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#; 这不是C#特有的。 you should use quotes even if you run it from the command line: 即使从命令行运行,也应使用引号:

python myscript.py "Rahul done good"

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

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