Skip to main content
 首页 » 编程设计

python之subprocess.Popen 系统属性 arg 中的空格

2025年02月15日8softidea

当将命令作为列表传递时,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']) 

请您参考如下方法:

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

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

它应该可以正常工作

引号只需要告诉命令行将其作为一个参数,但列表已经为您完成了,所以引号只是碍事

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