2011-10-19 26 views
3

我嘗試了很多東西,但由於某種原因,我無法正常工作。我正在嘗試使用Python腳本運行MS VS的dumpbin實用程序。爲什麼subprocess.Popen不工作

下面是我想什麼(什麼也沒有爲我工作)

1.

tempFile = open('C:\\Windows\\temp\\tempExports.txt', 'w') 
command = '"C:/Program Files/Microsoft Visual Studio 8/VC/bin/dumpbin" /EXPORTS ' + dllFilePath 
process = subprocess.Popen(command, stdout=tempFile) 
process.wait() 
tempFile.close() 

2.

tempFile = open('C:\\Windows\\temp\\tempExports.txt', 'w') 
command = 'C:/Program Files/Microsoft Visual Studio 8/VC/bin/dumpbin /EXPORTS ' + dllFilePath 
process = subprocess.Popen(command, stdout=tempFile) 
process.wait() 
tempFile.close() 

3.

tempFile = open('C:\\Windows\\temp\\tempExports.txt', 'w') 
process = subprocess.Popen(['C:\\Program Files\\Microsoft Visual Studio 8\\VC\\bin\\dumpbin', '/EXPORTS', dllFilePath], stdout = tempFile) 
process.wait() 
tempFile.close() 

做任何人有任何想法做我想要做的在Python中正確執行(dumpbin /EXPORTS C:\Windows\system32\kernel32.dll > tempfile.txt)?

+1

您可能想詳細說明它是如何工作的。你是否收到任何錯誤消息或任何東西? –

+0

你有沒有試過''C:\\ Program Files \\ Microsoft Visual Studio 8 \\ VC \\ bin \\ dumpbin.exe''? – rumpel

+0

@rumpel yup,也沒有工作。 –

回答

3

Popen的參數模式需要非shell調用的字符串列表和shell調用的字符串列表。這很容易解決。鑑於:

>>> command = '"C:/Program Files/Microsoft Visual Studio 8/VC/bin/dumpbin" /EXPORTS ' + dllFilePath 

要麼調用subprocess.Popenshell=True

>>> process = subprocess.Popen(command, stdout=tempFile, shell=True) 

或使用shlex.split創建參數列表:

>>> process = subprocess.Popen(shlex.split(command), stdout=tempFile) 
+0

都試過,但無法讓它工作。 btw dllpath是:'C:\\ Windows \\ system32 \\ user32.dll',也許它有幫助.. –

+0

note'shell = true'被認爲是2.7的安全風險 – thundergolfer

+0

@thundergolfer它完全取決於您是否控制該命令(如OP的問題)或來自不受信任的外部源:https://docs.python.org/2.7/library/subprocess.html#frequently-used-arguments –

1
with tempFile: 
    subprocess.check_call([ 
     r'C:\Program Files\Microsoft Visual Studio 8\VC\bin\dumpbin.exe', 
     '/EXPORTS', 
     dllFilePath], stdout=tempFile) 
+0

好了,現在我們確定它不起作用:) subprocess.CalledProcessError:Command'['C:\\ Program Files \\ Microsoft Visual Studio 8 \\ VC \\ bin \\ dumpbin.exe','/ EXPORTS','C:\\ Windows \\ system32 \ \ user32.dll']'返回非零退出狀態-1073741515 –

+0

當我這樣做時: subprocess.check_call('「C:\\ Program Files \\ Microsoft Visual Studio 8 \\ VC \\ bin \\ dumpbin。 exe「/ EXPORTS'+ dllFilePath,stdout = tempFile,shell = True) 退出代碼是相同的,但調試後我發現了那個dumpbin(o r cmd.exe)顯示消息:'文件名,目錄名稱或卷標語法不正確。' 並調試顯示當shell = True時,執行的命令是:C:\ WINDOWS \ system32 \ cmd.exe/c「」\「C:\ Program Files \ Microsoft Visual Studio 8 \ VC \ bin \ dumpbin.exe \「/ EXPORTS C:\ Window \ system32 \ user32.dll」「 –