我有一个用于从 Windows 命令行进行部署的命令。现在我需要从外部 python3.4 脚本运行相同的脚本。
命令是 C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild "D:\WebService\WebService.sln"/p:DeployOnBuild=true/p:PublishProfile="D:\WebService\Properties\PublishProfiles\MyDeployment.pubxml"/p:AllowUntrustedCertificate=true/p:UserName=name/p:Password=PASSWORD
.
我怎样才能做到这一点。我尝试了 subprocess
。但它不起作用。请帮助我。
最佳答案
您的问题似乎是 \
和 "
字符,因此请使用原始字符串。此外,使用列表更安全:
proc = subprocess.Popen(
[r"C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild",
r"D:\WebService\WebService.sln",
r"/p:DeployOnBuild=true",
r"/p:PublishProfile=D:\WebService\Properties\PublishProfiles\MyDeployment.pubxml",
r"/p:AllowUntrustedCertificate=true",
r"/p:UserName=name",
r"/p:Password=PASSWORD"])
proc.wait()
严格来说,您不需要所有这些参数的原始字符串,但使用 Windows 路径这样做更安全。 如果您嵌入了空格(作为第一个参数),则只需要内部双引号。这里我们不使用 shell,如果您设置 shell=True
作为参数需要一个。在 Windows 上使用 shell 的一个原因是为了文件名关联,但您在这里似乎没有使用它。
关于python - 从python3.4执行windows命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30637465/