从 python3.4 执行 windows 命令
Execute windows command from python3.4
我有一个用于从 windows 命令行进行部署的命令。现在我需要 运行 来自外部 python3.4 脚本的相同内容。
命令是C:\Program Files (x86)\MSBuild.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
。但这不是working.Please帮我。
你能post一些你目前尝试过的代码吗?
subprocess 模块应该能够处理这个问题,比如
theproc = subprocess.Popen(["COMMAND HERE"])
theproc.communicate()
或者您可以尝试使用 shell 标志
theproc = subprocess.Popen(["COMMAND HERE"], shell=True)
您的问题似乎是 \
和 "
字符,因此请使用原始字符串。另外,使用列表更安全:
proc = subprocess.Popen(
[r"C:\Program Files (x86)\MSBuild.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 的原因是为了文件名关联,但您似乎没有在这里使用它。
我有一个用于从 windows 命令行进行部署的命令。现在我需要 运行 来自外部 python3.4 脚本的相同内容。
命令是C:\Program Files (x86)\MSBuild.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
。但这不是working.Please帮我。
你能post一些你目前尝试过的代码吗?
subprocess 模块应该能够处理这个问题,比如
theproc = subprocess.Popen(["COMMAND HERE"])
theproc.communicate()
或者您可以尝试使用 shell 标志
theproc = subprocess.Popen(["COMMAND HERE"], shell=True)
您的问题似乎是 \
和 "
字符,因此请使用原始字符串。另外,使用列表更安全:
proc = subprocess.Popen(
[r"C:\Program Files (x86)\MSBuild.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 的原因是为了文件名关联,但您似乎没有在这里使用它。