简体   繁体   English

为什么subprocess.Popen不起作用

[英]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. 我试图使用Python脚本运行MS VS的dumpbin实用程序。

Here are what I tried (and what did not work for me) 这是我尝试过的(以及对我不起作用的东西)

1. 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. 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. 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? 有没有人有任何想法在Python中正确地做我想做的事( dumpbin /EXPORTS C:\\Windows\\system32\\kernel32.dll > tempfile.txt )?

The argument pattern for Popen expect a list of strings for non-shell calls and a string for shell calls. Popen的参数模式期望非shell调用的字符串列表和shell调用的字符串。 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 : 使用shell=True调用subprocess.Popen

>>> process = subprocess.Popen(command, stdout=tempFile, shell=True)

or use shlex.split to create an argument list: 或使用shlex.split创建参数列表:

>>> process = subprocess.Popen(shlex.split(command), stdout=tempFile)
with tempFile:
    subprocess.check_call([
        r'C:\Program Files\Microsoft Visual Studio 8\VC\bin\dumpbin.exe',
        '/EXPORTS', 
        dllFilePath], stdout=tempFile)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此命令与os.system()一起使用,而不与subprocess.Popen()一起使用? - Why does this command work with os.system() but not subprocess.Popen()? 为什么subprocess.Popen无法与find -exec一起使用? - Why does subprocess.Popen not work with find -exec? Python subprocess.Popen:使用“&gt;”重定向不起作用 - Python subprocess.Popen: redirection with ">" does not work 为什么带有shell = True的subprocess.Popen()在Linux和Windows上的工作方式不同? - Why does subprocess.Popen() with shell=True work differently on Linux vs Windows? 为什么subprocess.Popen阻塞? - Why is subprocess.Popen blocking? 为什么`script.py &lt;(cat * .gz)`在python 2中与subprocess.Popen一起使用,但在python 3中却不起作用? - Why does `script.py <(cat *.gz)` work with subprocess.Popen in python 2 but not python 3? 为什么subgs.Popen在args是序列时不起作用? - Why subprocess.Popen doesn't work when args is sequence? 为什么打开gnugo时subprocess.Popen挂起? - Why does subprocess.Popen hang when opening gnugo? 为什么命令及其 arguments 必须在 subprocess.Popen 的列表中? - Why does the command and its arguments have to be in a list for subprocess.Popen? 为什么subprocess.Popen会阻止SimpleHTTPServer.SimpleHTTPRequestHandler的响应 - Why does subprocess.Popen block the response of SimpleHTTPServer.SimpleHTTPRequestHandler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM