为什么subprocess.Popen不起作用

Hay*_*tuk 4 python subprocess dumpbin

我尝试了很多事情,但是由于某种原因,我无法使事情正常进行。我正在尝试使用Python脚本运行MS VS的dumpbin实用程序。

这是我尝试过的(对我不起作用的)

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()
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

有没有人对dumpbin /EXPORTS C:\Windows\system32\kernel32.dll > tempfile.txt在Python中正确执行我想做的事情有任何想法?

Ray*_*ger 5

Popen的参数模式需要非shell调用的字符串列表和shell调用的字符串列表。这很容易解决。鉴于:

>>> command = '"C:/Program Files/Microsoft Visual Studio 8/VC/bin/dumpbin" /EXPORTS ' + dllFilePath
Run Code Online (Sandbox Code Playgroud)

要么调用subprocess.Popen有shell=True:

>>> process = subprocess.Popen(command, stdout=tempFile, shell=True)
Run Code Online (Sandbox Code Playgroud)

或使用shlex.split创建参数列表:

>>> process = subprocess.Popen(shlex.split(command), stdout=tempFile)
Run Code Online (Sandbox Code Playgroud)

  • 注意自2.7起,shell = true被视为安全风险 (2认同)