【问题标题】:subprocess.Popen spaces within system property arg系统属性 arg 中的 subprocess.Popen 空间
【发布时间】:2017-07-13 00:36:11
【问题描述】:

将命令作为列表传递时,subprocess.Popen 将自动引用有空格的参数。但是,如果我使用具有如下空格的系统属性运行 java:

-Dwebdriver.firefox.bin="C:\Program Files (x86)\Mozilla Developer Preview\firefox.exe"

,会报错: 'C:\Program' 无法识别错误 ,我认为这是因为 Popen 会在看到空格时在整个参数周围插入引号,并转义其余的双引号。如果我想继续使用 Popen 命令,我不确定如何解决这个问题:

subprocess.Popen([
  'cmd.exe', '/C', 'C:\\Program Files\\java\\jre7\\bin\\java.exe',
  '-Dwebdriver.firefox.bin="C:\\Program Files (x86)\\Mozilla Developer Preview\\firefox.exe"',
  '-jar', 'C:\\Users\\testing\\Temp\\SeleniumServer.jar'])

【问题讨论】:

  • 你想做什么详细说明......
  • 为什么是cmd.exe?添加 shell 解释层会引入原本不会有的复杂性。
  • 我认为需要 cmd.exe 因为我没有使用 shell=True。

标签: python subprocess


【解决方案1】:

将该参数作为一个不带引号的列表项提供

[..., '-Dwebdriver.firefox.bin=C:\\Program Files (x86)\\Mozilla Developer Preview\\firefox.exe', ...]

它应该可以正常工作

引号只需要告诉命令行将它作为一个参数,但列表已经为你做了,所以引号只是在路上

看看这个答案:ffmpeg through python subprocess fails to find camera

【讨论】:

  • 没有引号,我仍然收到错误消息。我加引号的原因是我希望它模仿在 cmd 中运行实际命令,该命令有效: "C:\Program Files\java\jre7\bin\java.exe" "-Dwebdriver.firefox.bin=\"C :\Program Files (x86)\Mozilla Developer Preview\firefox.exe\"" -jar C:\Users\testing\AppData\Local\Temp\SeleniumServer.jar
  • 经过进一步测试,这似乎不是python Popen问题,而是windows cmd问题。我之前的测试只对 Cygwin 有效,但在 cmd 中没有,所以我会先尝试解决这个问题。
  • 根据stackoverflow.com/questions/12891383/…看来,正确引用命令的方式是:cmd.exe /C ""C:\Program Files\java\jre7\bin\java.exe" " -Dwebdriver.firefox.bin=C:\Program Files (x86)\Mozilla Developer Preview\firefox.exe" -jar C:\Users\testing\AppData\Local\Temp\SeleniumServer.jar" ,这对于自动Popen 对参数列表执行的转换。
猜你喜欢
  • 2013-06-06
  • 1970-01-01
  • 2018-11-24
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
相关资源
最近更新 更多