如何使用Python子进程将多个输入写入自定义exe程序 [英] How to use Python subprocess to write multiple inputs into custom exe program

查看:99
本文介绍了如何使用Python子进程将多个输入写入自定义exe程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开一个可执行文件,该文件可打开 HEC .dss 数据库文件.但是,我似乎只能在打开 exe 后让它读取一个参数,然后它不读取任何其他内容.有什么办法可以强制它继续插入命令.

I am trying to open a executable that opens a HEC .dss database file. However, I can only seem to get it to read one argument after opening the exe and then it doesn't read anything else. Is there any way to force it to keep inserting commands.

这个 exe 有一些独特的功能,其中包括第一个命令询问您要读取的 DSS 文件.然后您可以输入一个命令来创建输出 txt 文件,它将为其余命令写入该文件.到目前为止,我能够做的是启动程序并在 exe 中运行一个命令(mydss 变量).但是,在读取第一个命令后,命令提示符中不会使用任何其他命令.我觉得我在这里错过了一些东西.代码如下:

This exe has some unique features to it, which include that the first command asks what DSS file you are going to read. Then you can input a command to create the output txt file that it will write to for the rest of the commands. What I've been able to do so far is to start the program and run one command into the exe (the mydss variable). However, after that first command is read, none of the other commands are used in the command prompt. I feel like I'm missing something here. Here is the code:

##Testing on how to run and use the DSSUTL program
import subprocess
from subprocess import PIPE, STDOUT

DSSUTL = "C:\Users\sduncan\Documents\HEC-DSS\HEC-DSSVue-2_0_1\FromSivaSel\DSSUTL.exe"
mydss = "C:\Users\sduncan\Documents\HEC-DSS\HEC-DSSVue-2_0_1\FromSivaSel\\forecast.dss"
firstLine = "WR.T TO=PythonTextOutput.txt"
commandLine = "WR.T B=SHAVER RESERVOIR-POOL C=FLOW-IN E=1HOUR F=10203040"
myList = [firstLine, commandLine]
ps = subprocess.Popen([DSSUTL, mydss, myList[1], myList[0]], shell=True)

我也试过包括 stdin=subprocess.PIPE,但这只会导致 exe 打开并且它是空白的(当我用上面的代码打开它时,我可以阅读它并看到mydss 变量被正确读取).当我使用 stdoutsterr 时,程序只会打开和关闭.

I've also tried including stdin=subprocess.PIPE, but that only leads to the exe opening and it is blank (when I open it with the code above I can read it and see that the mydss variable was read correctly). When I used stdout or sterr, the program only opens and closes.

我也试过在 stdin=PIPE 开启时使用代码:

I've also tried using the code when the stdin=PIPE was turned on with:

ps.stdin.write(myList[1])
ps.stdin.write(myList[0])
ps.communicate()[0]

然而,它没有读取程序中的任何内容.这个程序像命令提示符一样运行,但是,它不是典型的 cmd,因为它被用来读取 DSS 文件类型并生成一个文本文件,其中包含来自 commandLine 变量中的搜索列表

However, it did not read anything in the program. This program runs like a command prompt, however, it's not the typical cmd as it was made to read the DSS filetype and produce a text file with the list from searches like in the commandLine variable

很高兴知道我可以做些什么来修复代码,以便我可以输入额外的命令.任何帮助了解如何事件检查命令是否由此 exe 发送或处理.最终,我将向 exe 文件添加更多命令以打印到文本文件,因此如果有任何方法让 python 写入 exe 会有所帮助.

It would be nice to know what I could do to fix the code so that I could input the extra commands. Any help to know how to event check if the commands were being sent or processed by this exe. Eventually, I will be adding many more commands to the exe file to print to the text file, so if there is any way to get python to write to the exe that would help.

推荐答案

@tdelaney, @eryksun 感谢您的评论,您对管道和延迟的评论真的很有帮助.我能够通过使用此代码解决问题:

@tdelaney, @eryksun Thank you for commenting, your comments about the pipes and delay really helped. I was able to fix the problem by using this code:

##Testing on how to run and use the DSSUTL program
import subprocess
from subprocess import PIPE, STDOUT
import time

DSSUTL = "C:\Users\sduncan\Documents\HEC-DSS\HEC-DSSVue-2_0_1\FromSivaSel\DSSUTL.exe"
mydss = "C:\Users\sduncan\Documents\HEC-DSS\HEC-DSSVue-2_0_1\FromSivaSel\\forecast.dss"
location = "WR.T TO=PythonTextOutput.txt" + " WR.T B=SHAVER RESERVOIR-POOL C=FLOW-IN E=1HOUR F=10203040" + "\n"
filecontent1 = "WR.T B=FLORENCE RESERVOIR-POOL C=FLOW-IN E=1HOUR F=10203040" + "\n"
filecontent2 = "WR.T B=HUNTINGTON LAKE-POOL C=FLOW-IN E=1HOUR F=10203040" + "\n"
filecontentList = [filecontent1, filecontent2]
myList = [DSSUTL, mydss] # commandLine, location
ps = subprocess.Popen(myList , shell=False, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
time.sleep(1)
# input into stdin
ps.stdin.write(location)
time.sleep(1)
ps.stdin.write(filecontent1)
time.sleep(1)
ps.stdin.write(filecontent2)
time.sleep(1)
print ps.communicate()[0]
# End Script

通过使用管道与程序对话并延迟时间似乎可以解决问题并允许我与控制台对话.即使控制台显示为空白,通过打印communication() 命令,它也会输出控制台所做的事情并生成包含所需系列的文本文件.

By using the pipes to talk to the program and putting a time delay seemed to fix the problem and allowed me to talk to the console. Even though the console display is blank, by printing the communicate() command, it outputs what the console did and produce the text file with the wanted series.

感谢您将我推向正确的方向!

Thanks for pushing me in the right direction!

这篇关于如何使用Python子进程将多个输入写入自定义exe程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆