2017-04-07 83 views
0

我想用參數啓動ReadyApi的testrunner.bat。我試圖通過一些XML部分(PeriodEnd在下面的代碼)作爲參數傳遞給subprocess.Popen:Python:將「<」傳遞給Popen

argslist = ['C:/Program Files/SmartBear/ReadyAPI-1.9.0/bin/testrunner.bat', 
    '-a', '-s', 'TestSuite', '-c', 'TestCase', '-f', 'C:/temp/', '-P', 
    'PeriodEnd=<PeriodEnd>2017-04-11T00:00:00.000Z</PeriodEnd>', 
    'C:/temp/soapui-project.xml'] 
proc = Popen(argslist, stdout=PIPE, stderr=PIPE) 

這將產生以下錯誤:

The system cannot find the file specified. 

我發現,認爲「<」和「>」是問題。我怎樣才能擺脫它們或將它們傳遞給Popen?

+0

我猜的Windows願意爲你把周圍的特定參數雙引號,但是我沒有Wintendo盒測試此上。 – tripleee

+0

您是否嘗試直接在CMD中運行該命令? –

+0

我也嘗試過'「」PeriodEnd = 2017-04-11T00:00:00.000Z「」'但這沒有幫助。 – Tobias

回答

1

在CMD轉義字符^

C:\> echo asdf<123> 
The syntax of the command is incorrect. 

C:\> echo asdf^<123^> 
asdf<123> 
+0

是的,子進程'list2cmdline'使用VC++規則來創建命令行。它不知道如何轉義cmd.exe的命令行,特別是考慮到它們通常必須經過精心設計,不僅可以存活cmd,還可以通過命令行解析通過cmd運行的任何命令行。如果您使用'Popen'直接運行批處理文件或使用'shell = True',我建議使用經過驗證的命令行字符串而不是args列表。 – eryksun