Why does subprocess.Popen not work

18k views Asked by At

I tried a lot of things but for some reason I could not get things working. I am trying to run dumpbin utility of MS VS using a Python script.

Here are what I tried (and what did not work for me)

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

does anyone have any idea on doing what i am trying to do (dumpbin /EXPORTS C:\Windows\system32\kernel32.dll > tempfile.txt) correctly in Python?

2

There are 2 answers

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

The argument pattern for Popen expect a list of strings for non-shell calls and a string for shell calls. This is easy to fix. Given:

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

Either call subprocess.Popen with shell=True:

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

or use shlex.split to create an argument list:

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