Why does subprocess.Popen not work

12,821

Solution 1

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)

Solution 2

with tempFile:
    subprocess.check_call([
        r'C:\Program Files\Microsoft Visual Studio 8\VC\bin\dumpbin.exe',
        '/EXPORTS', 
        dllFilePath], stdout=tempFile)
Share:
12,821
Hayri Uğur Koltuk
Author by

Hayri Uğur Koltuk

software engineer

Updated on June 27, 2022

Comments

  • Hayri Uğur Koltuk
    Hayri Uğur Koltuk almost 2 years

    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?

  • Hayri Uğur Koltuk
    Hayri Uğur Koltuk over 12 years
    tried both but could not get it working. btw dllpath is : 'C:\\Windows\\system32\\user32.dll' , maybe it helps..
  • Hayri Uğur Koltuk
    Hayri Uğur Koltuk over 12 years
    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 -1073741515
  • Hayri Uğur Koltuk
    Hayri Uğur Koltuk over 12 years
    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""
  • thundergolfer
    thundergolfer over 7 years
    note shell=true is considered a security risk as of 2.7
  • Raymond Hettinger
    Raymond Hettinger over 7 years
    @thundergolfer 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/…