Why does subprocess.Popen not work

Hayri Uğur Koltuk picture Hayri Uğur Koltuk · Oct 19, 2011 · Viewed 12.1k times · Source

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?

Answer

Raymond Hettinger picture Raymond Hettinger · Oct 19, 2011

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)