簡體   English   中英

在python中執行shell命令:字符串錯誤

[英]Executing shell command in python: String error

我想在每個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"

當我在命令提示符下手動輸入此行時,它將起作用。 它啟動一個新的外殼程序,執行批處理文件vcvarsall.bat (帶有參數x86),然后在外殼程序內執行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:”?有人知道如何解決此問題?

試試這個:

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

您不能在“”-“內部使用(它將被轉義)

命令行上的"xx"等效於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"')

我從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. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM