Python中的Popen 3

2024-06-08 22:27:10 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的程序中的一段代码。在

我试着打开命令行.exe并将命令传递到单独的程序,捕获输出并解析它,而不必加载命令行.exe每一次。在

所有的例子我都找到了使用python2的地方,python3中关于管道的一些更改,使我不确定出了什么问题。在

#DOScmd is a list of command line parameters to type into command shell.

p = subprocess.Popen('cmd.exe',
                 stdout=subprocess.PIPE,
                 stdin=subprocess.PIPE,
                 shell=True,
                 bufsize=0)


myCall = ' '.join(DOScmd) + '\n'
p.stdin.write( bytes(myCall, 'UTF-8') )
searchLines =  p.stdout.readlines()               
print(searchLines)

我正在调用一个程序蝴蝶结.exe. 现在,蝴蝶结.exe我这么做的时候会崩溃。我想我可能激怒了I/O神。感谢任何帮助。在


Tags: 代码命令行命令程序stdinstdoutshellexe
1条回答
网友
1楼 · 发布于 2024-06-08 22:27:10

I am trying to open cmd.exe on Windows and pass commands to a separate program and capture the output and parse it WITHOUT having to load cmd.exe every time.

除非你想在上运行所有的命令

from subprocess import check_output

for cmd in ["first.exe", "arg1", "arg2"], ["second.exe", ".."]:
    output = check_output(cmd)
    do_whatever_you_like_with(output)

相关问题 更多 >