为什么subprocess.Popen不起作用

时间:2011-10-19 15:36:23

标签: python dumpbin subprocess

我尝试了很多东西,但由于某种原因,我无法让事情奏效。我正在尝试使用Python脚本运行MS VS的dumpbin实用程序。

以下是我尝试的内容(以及对我不起作用的内容)

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)?

2 个答案:

答案 0 :(得分:5)

Popen 的参数模式需要非shell调用的字符串列表和shell调用的字符串。这很容易解决。给出:

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

使用shell=True调用 subprocess.Popen

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

或使用 shlex.split 创建参数列表:

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

答案 1 :(得分:1)

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