【发布时间】:2022-08-13 21:43:38
【问题描述】:
在 Python 中,使用 subprocess.Popen,当命令及其参数采用列表形式时,是否可以将文字引号作为参数传递?
我会进一步解释我的意思。某些命令在其参数中需要文字引号,例如\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" --profile-directory=\"Profile 1\"
为简单起见,我将使用 calc.exe,因为它在路径中。
import time
import subprocess
proc=subprocess.Popen(\"calc.exe\"+\" \"+\'--profile-directory=\"Profile 3\"\')
proc2=subprocess.Popen([\"calc.exe\",\'--profile-directory=\"Profile 4\"\'])
time.sleep(3)
proc.wait()
proc2.wait()
现在查看在任务管理器中或通过 wmic 可见的命令行中的差异。
C:\\Users\\User>wmic 进程 where caption=\"calc.exe\" 获取命令行 | findstr 计算
c:\\windows\\system32\\calc.exe --profile-directory=\"配置文件 3\"
c:\\windows\\system32\\calc.exe \"--profile-directory=\\\"配置文件4\\\"\"
C:\\用户\\用户>
添加
一个建议假设--profile-directory=\"Profile 1\" 与--profile-directory \"Profile 1\" 相同,即假设您可以将= 替换为空格并且chrome 的工作方式相同。但事实并非如此。所以写subprocess.Popen([\"C:\\...\\chrome.exe\", \"--profile-directory\", \"Profile 3\"]) 确实会产生\"C:\\....\\chrome.exe\" --profile-directory \"Profile 1\" 但这不会起作用.. 它会导致chrome 要么根本不打开,要么打开一个提供配置文件供点击的浏览器窗口。等号是必须的。
另一个建议确实
subprocess.Popen(
\" \".join(
[
\"C:\\\\Program Files\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe\",
\'--profile-directory=\"Person 1\"\',
]
)
那不是将列表传递给 Popen,而是将列表传递给 join,而 join 是将其转换为字符串。
另一个建议是
subprocess.Popen(\'C:\\\\Program Files (x86)\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe --profile-directory=\"Profile 3\"\')
那是使用字符串。但是正如您从我的问题中看到的那样,我使用字符串对其进行了管理。我问的是使用列表。
另一个建议建议\"--profile-directory=\'Profile 1\'\"
如果我使用 --profile-directory=\"Profile 1\" 运行 chrome,我会得到一个我有时使用的特定配置文件。但是,如果使用 \"--profile-directory=\'Profile 1\'\" 运行 chrome,那么它不会加载该配置文件。它加载一个空白配置文件。并且去 chrome://version 显示 \"\'profile 1\'\" 而不是 \"profile 1\" 这就像一个不同的配置文件,就像你可能已经说过 chrome.exe --profile-directory=\"profile A\"
另一个建议建议
subprocess.Popen(
[
\"C:\\\\Program Files (x86)\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe\",
\"--profile-directory=Profile 1\",
]
这很有趣,因为它确实\"C:\\...chrome.exe\" \"--profile-directory=Profile 1\"
它确实使用指定的配置文件加载 chrome。虽然它不会尝试传递文字引号!
我的问题询问何时传递文字引号。就好像它假设它是一个 linux shell 并在它之前插入一个反斜杠,这在 linux 中将确保引号使其通过 shell 并进入正在运行的程序。虽然我不确定它甚至会转到 linux 上的 linux shell。例如在 Windows 上,如果我在其中粘贴一个 cmd 转义字符,例如 ^ 所以 \"--pro^file-directory=Profile 1\" 那么 ^ 只是按字面意思传递。所以 cmd shell 不会干预。