Why does subprocess.Popen not work
Asked Answered
G

2

7

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?

Gnu answered 19/10, 2011 at 15:36 Comment(4)
You may want to elaborate on how it doesn't work. Do you get any error messages or anything?Syman
Have you tried 'C:\\Program Files\\Microsoft Visual Studio 8\\VC\\bin\\dumpbin.exe'?Alkoran
@Alkoran yup, didn't work either.Gretta
@Thomas K well the temp file is empty, this is what i mean by 'does not work'Gretta
S
8

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)
Silvery answered 19/10, 2011 at 17:12 Comment(3)
tried both but could not get it working. btw dllpath is : 'C:\\Windows\\system32\\user32.dll' , maybe it helps..Gretta
note shell=true is considered a security risk as of 2.7Eigenvalue
@Eigenvalue It depends entirely on whether you control the command (as in the OP's question) or from an untrusted external source: docs.python.org/2.7/library/…Silvery
R
2
with tempFile:
    subprocess.check_call([
        r'C:\Program Files\Microsoft Visual Studio 8\VC\bin\dumpbin.exe',
        '/EXPORTS', 
        dllFilePath], stdout=tempFile)
Rimrock answered 19/10, 2011 at 23:12 Comment(2)
ok now we are sure that it does not work :) subprocess.CalledProcessError: Command '['C:\\Program Files\\Microsoft Visual Studio 8\\VC\\bin\\dumpbin.exe', '/EXPORTS', 'C:\\Windows\\system32\\user32.dll']' returned non-zero exit status -1073741515Gretta
when i do this: subprocess.check_call('"C:\\Program Files\\Microsoft Visual Studio 8\\VC\\bin\\dumpbin.exe" /EXPORTS ' + dllFilePath, stdout=tempFile, shell=True) the exit code is the same, but after debugging i found out that dumpbin (or cmd.exe) displays the message: 'The filename, directory name, or volume label syntax is incorrect.' and debugging showed that when shell=True the command that is executed is: C:\WINDOWS\system32\cmd.exe /c ""\"C:\Program Files\Microsoft Visual Studio 8\VC\bin\dumpbin.exe\" /EXPORTS C:\Window\system32\user32.dll""Gretta

© 2022 - 2024 — McMap. All rights reserved.