我想在每个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:??任何人都知道如何解决这个问题?
答案 0 :(得分:0)
试试这个:
'"C:\Program Files(x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86 & msbuild ALL_BUILD.vcxproj'
你不能在里面使用“” - “(它将被转义)
答案 1 :(得分:0)
命令行上的"x x"
等同于subprocess.call中的'x x'
。
最终你可以留下一些"
。
然而..你试过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"')
答案 2 :(得分:0)
以下来自Windows命令行参考我觉得相关:
*使用多个命令
您可以使用命令分隔符&& 分隔的多个命令 字符串,但您必须用引号将它们括起来(例如, “命令&安培;&安培;命令&安培;&安培;命令”。)
*处理引号
如果指定/ 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.