参数中带有空格的 Python 子进程调用在 Windows 上不起作用 [英] Python subprocess call with whitespaces in arguments doesn't work on Windows

查看:31
本文介绍了参数中带有空格的 Python 子进程调用在 Windows 上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个 java 命令,它采用类路径和其他具有空格的文件位置.Windows 似乎不喜欢它.

I am running a java command which takes the classpath and other file locations which have a white space. Windows doesn't seem to like it.

我有从 C:\Program Files\Splunk 运行的程序,它有一个空格

I have the program running from C:\Program Files\Splunk , which has a white space

这是我的命令

c1 = os.path.join(appdir, "bin", "apache-flume-1.3.1-bin", "lib", "*")
c2 = os.path.join(appdir, "bin", "apache-flume-1.3.1-bin", "lib", "flume-ng-node-1.3.1.jar")
c3 = os.path.join(appdir, "bin", "dtFlume.jar")

classpath = c1 + os.pathsep + c2 + os.pathsep + c3
log4j = os.path.join(appdir,"bin", "apache-flume-1.3.1-bin", "conf", "log4j.properties")
flumeconf = os.path.join(appdir,"bin","flume-conf.properties");

变体 1 - 不起作用

cmdline = "java  -Xmx20m -Dlog4j.configuration=file:" + log4j + " -cp " + classpath + " org.apache.flume.node.Application -f " + flumeconf + " -n agent1"


try:
    p = subprocess.Popen("%s" %(cmdline),shell=True,stdout=subprocess.PIPE)

变体 2 - 不起作用

cmdline_1 = "java  -Xmx20m -Dlog4j.configuration=file:"
cmdline_2 = log4j 
cmdline_3 = " -cp " 
cmdline_4 = classpath
cmdline_5 = " org.apache.flume.node.Application -f " 
cmdline_6 = flumeconf
cmdline_7 = " -n agent1"


try:
    p = subprocess.Popen('""%s" "%s" "%s" "%s" "%s" "%s" "%s""' %(cmdline_1,cmdline_2,cmdline_3,cmdline_4,cmdline_5,cmdline_6,cmdline_7),shell=True,stdout=subprocess.PIPE)

变体 3 - 不起作用

cmdline_1 = "java  -Xmx20m -Dlog4j.configuration=file:"
cmdline_2 = log4j 
cmdline_3 = " -cp " 
cmdline_4 = classpath
cmdline_5 = " org.apache.flume.node.Application -f " 
cmdline_6 = flumeconf
cmdline_7 = " -n agent1"


try:
    p = subprocess.Popen("%s %s %s %s %s %s %s" %(cmdline_1,cmdline_2,cmdline_3,cmdline_4,cmdline_5,cmdline_6,cmdline_7),shell=True,stdout=subprocess.PIPE)

每次它给我相同/类似的错误,找不到主类,路径在 C:\Program 处被截断

Every time it gives me the same/similar error, couldn't find main class, and the path is truncated at the C:\Program

"C:\Program Files\Splunk\etc\apps\APM_dynatrace\bin\runFlume.py"" 错误:无法找到或加载主类 Files\Splunk\etc\apps\APM_dynatrace\bin\apache-flume-1.3.1-bin\conf\log4j.properties

"C:\Program Files\Splunk\etc\apps\APM_dynatrace\bin\runFlume.py"" Error: Could not find or load main class Files\Splunk\etc\apps\APM_dynatrace\bin\apache-flume-1.3.1-bin\conf\log4j.properties

推荐答案

不要使用 shell=True.文档声明您只需要shell=True 在 Windows 上,如果使用实际内置到 shell 中的命令:

Don't use shell=True. The docs state you only need shell=True on Windows if using a command actually built-into the shell:

您唯一需要在 Windows 上指定 shell=True 的时间是您希望执行的命令内置于 shell 中(例如 dir 或复制).您不需要 shell=True 来运行批处理文件或基于控制台的可执行.

The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

如果您不使用 shell=True,您可以将参数作为列表传递,而不必担心 shell 会错误地处理空格.

If you aren't using shell=True, you can pass the arguments as a list, and not worry about spaces being treated incorrectly by the shell.

p = subprocess.Popen(['java', '-Xmx20m', '-Dlog4j.configuration=file:%s' % log4j,
                      '-cp', classpath, 'org.apache.flume.node.Application', 
                      '-f', flumeconf, '-n', 'agent1'], stdout=subprocess.PIPE)

这篇关于参数中带有空格的 Python 子进程调用在 Windows 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆