如何使用python的子进程安装MSI,传递包含空格的安装目录?

时间:2015-06-09 08:12:28

标签: python windows-installer

当我尝试使用类似于

的形式的Python命令运行MSI时

subprocess.call([" msiexec.exe"," / i"," myinstaller.msi"," / log&# 34;," myinstalllog.log"," INSTALLDIR = \" C:\ Program Files \ InstallDirectory \""])

无法安装,而只是我得到Windows Installer弹出窗口,解释可用的命令行选项。如果我使用不包含空格的不同INSTALLDIR参数(并且没有转义引号),它就可以工作。我也可以从 os.system 运行msi,但这不适合我的预期目的,因为我需要Python等到安装完成。

1 个答案:

答案 0 :(得分:-1)

重新阅读你所说的东西之后问题实际上并不是你的引号,但字符串中的斜线字符当它们不应该作为转义字符时,我为测试做的是创建一个父脚本:

#!python3

import subprocess

if __name__ == "__main__":
    subprocess.call(["py.exe", "child.py", "/i", "myinstaller.msi", "/log", "myinstalllog.log", "INSTALLDIR=\"C:\\Program Files\\InstallDirectory\""])

和一个子脚本:

#!python3

import sys
print("Argument count: %i" % len(sys.argv))
for item in sys.argv:
    print(item)

thing = input("Press return to exit")

请注意,在父脚本中我已更改:

"INSTALLDIR=\"C:\Program Files\InstallDirectory\""

为:

"INSTALLDIR=\"C:\\Program Files\\InstallDirectory\""

这样斜杠绝对是斜线而不是逃避。