【问题标题】:Execute windows command from python3.4从python3.4执行windows命令
【发布时间】:2015-08-18 16:31:19
【问题描述】:

我有一个用于从 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 。但它不起作用。请帮帮我。

【问题讨论】:

  • I tried subprocess. But it's not working
  • @inspectorG4dget:我试过subprocess.call("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")。它给了我语法错误。

标签: python windows cmd subprocess python-3.4


【解决方案1】:

您的问题似乎是\" 字符,因此请使用原始字符串。此外,使用列表更安全:

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 的一个原因是文件名关联,但您似乎没有在这里使用它。

【讨论】:

  • 如果我将当前工作目录存储在变量 pwd 中,是否可以在此答案中提供相对路径
  • @NevinRaj:取决于你如何使用它。通常是的,所有文件名都可以是相对的,并且当前工作目录由子进程继承(默认情况下),因此您不需要pwd。但是,有些程序需要绝对路径,而且您通常无法确定工作目录是什么,这取决于用户。
  • 不要手动添加引号。 subprocess.Popen 致电 subprocess.list2cmdline 为您处理此问题。此函数假定由 Microsoft 的 C 运行时和 CommandLineToArgvW 实现的 argv 解析规则。 大多数 Windows 程序都遵守这些规则。如果程序有自己的命令行解析规则,你必须将命令作为字符串传递。
  • @eryksun:你是对的,但如果shell=True,则需要它们。
  • 在 Windows 上,Popen 使用 args = list2cmdline(args),即使是 shell 命令,所以永远不要在列表中手动添加引号。也就是说,您不应该将列表用于shell 命令。在 POSIX 系统上,它不太可能是您想要的(参数被传递给 shell 本身,并用作 $0$1 等)。在 Windows 上,cmd.exe 有自己古怪的引用和转义规则,而 Python 目前没有实现 cmd 的“逻辑”的函数(受尽折磨)。
【解决方案2】:

你能发布一些你迄今为止尝试过的代码吗?

子进程模块应该能够处理这个问题,比如

theproc = subprocess.Popen(["COMMAND HERE"])
theproc.communicate()

或者你可以尝试使用 shell 标志

theproc = subprocess.Popen(["COMMAND HERE"], shell=True)

【讨论】:

  • 我试过theproc = subprocess.Popen([sys.executable, "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=Admin /p:Password=me@321"]) ^ SyntaxError: invalid syntax Process finished with exit code 1
  • 你能试试theproc = subprocess.Popen("C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild \" D:\WebService\WebService.sln\" /p:DeployOnBuild=true /p:PublishProfile=\"D:\We bService\Properties\PublishProfiles\MyDeployment.pubxml\" /p:AllowUntrustedCer tificate=true /p:UserName=Admin /p:Password=me@321") theproc.communicate()你的命令字符串有多个双引号,所以字符串在你想要的之前结束。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 2012-07-15
相关资源
最近更新 更多