Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does subprocess.Popen not work

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?

like image 422
Hayri Uğur Koltuk Avatar asked Oct 19 '11 15:10

Hayri Uğur Koltuk


People also ask

How does subprocess Popen work?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

How do I use subprocess Popen in Python?

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly. Run the command described by args. Wait for command to complete, then return a CompletedProcess instance.

Is Popen deprecated?

popen() is not recommended. Deprecated since version 2.6: This function is obsolete.

Does subprocess Popen block?

Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.


2 Answers

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)
like image 115
Raymond Hettinger Avatar answered Oct 13 '22 02:10

Raymond Hettinger


with tempFile:
    subprocess.check_call([
        r'C:\Program Files\Microsoft Visual Studio 8\VC\bin\dumpbin.exe',
        '/EXPORTS', 
        dllFilePath], stdout=tempFile)
like image 24
jfs Avatar answered Oct 13 '22 04:10

jfs