1

我想在每个 Python 的 windows shell cmd.exe 上执行一个批处理文件和一个命令。它的这个命令:

$cmd.exe /k ""C:\Program Files(x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86 & msbuild ALL_BUILD.vcxproj"

当我在命令提示符下手动输入此行时,它可以工作。它启动一个新的 shell,执行批处理文件vcvarsall.bat(带有参数 x86),然后在 shell 中执行 msbuild ALL_BUILD.vcxproj。引用路径是因为它包含空格。

现在,如果我尝试使用以下命令在 python 中执行此命令:

subprocess.call(["cmd.exe", "/k", '"C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/vcvarsall.bat" x86 & msbuild ALL_BUILD.vcxproj'])

我总是在控制台上收到此错误:

找不到命令 "\"C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/vcvarsall.bat\""。

为什么命令以 "\"C: 而不是我输入的 "C: ?? 开头?有人知道我该如何解决这个问题吗?

4

3 回答 3

0

我拥有的 Windows 命令行参考中的以下内容似乎相关:

* 使用多个命令

您可以使用命令分隔符&&分隔的多个命令用于字符串,但必须将它们括在引号中(例如,“command&&command&&command”)。

* 处理引号

如果您指定 /c 或 /k,cmd 会处理字符串的其余部分,并且仅当满足以下所有条件时才会保留引号

o   You do not use /s. 
o   You use exactly one set of quotation marks. 
o   You do not use any special characters within the quotation marks (for 
    example: &<>( ) @ ^ |). 
o   You use one or more white-space characters within the quotation marks. 
o   The string within quotation marks is the name of an executable file. 
    If the previous conditions are not met, string is processed by examining the first 
    character to verify whether or not it is an opening quotation mark. If the first 
    character is an opening quotation mark, it is stripped along with the closing 
    quotation mark. Any text following the closing quotation marks is preserved.
于 2012-12-12T14:15:26.033 回答
0

试试这个:

 '"C:\Program Files(x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86 & msbuild ALL_BUILD.vcxproj'

你不能在 "" - " 里面使用(它会被转义)

于 2012-12-12T10:26:30.247 回答
0

on 命令行"x x"相当于一个'x x'in subprocess.call。最终你可以留下一些"

但是..你试过os.system吗?

import os

os.system('cmd.exe /k ""C:\Program Files(x86)\Microsoft Visual Studio 10.0\VC vcvarsall.bat" x86 & msbuild ALL_BUILD.vcxproj"')
于 2012-12-12T13:42:18.040 回答