run an executable using wexpect

折月煮酒 提交于 2019-12-08 02:11:50

问题


I have an executable (evapo.exe) which has to be called with an input file (inputs.prj), usually I do it using windows command prompt by just typing c:\myfiles\evapo inputs.prj (of course both the executable and input file located in myfiles folder)

Now I want to be able to do the same thing using python. Other similar questions here on SO suggested to use wexpect since other methods such as subprocess do not work when the executable askes for other informations to run (for my case the information being the input file). i tried:

import wexpect

wexpect.run(r'c:\myfiles\evapo.exe')

and python hangs..., please help me if anyone has an idea how i can combine the exe and input file as I do it using cmd.


回答1:


You don't need wexpect if all information you want to pass is a file name:

from subprocess import check_call

check_call(r'c:\myfiles\evapo.exe inputs.prj')



回答2:


I guess wexpect is a python port of pexpect and behaves similarly but works on windows.

I checked on the following: http://www.jjoseph.org/files/led_calibration/wexpect.py

If that is the case then any of the examples for pexpect should work.

run() command should be used when you just want to collect output. This does not work when you want to interact and provide inputs.

if you want to send the inputs, you will need to use spawn() function.

See the example in the code. Here the command asks for an input which is sent across by wexpect

child = wexpect.spawn('some command')
child.expect ('Password:')
child.sendline (mypassword)

Just go through following answers on pexpect and substitute it with wexpect.

  • Simplest way to run expect script from python
  • Need little assistance with pexpect module
  • Need little assistance with pexpect interaction

And also for windows following port has been suggested as working one:

  • https://bitbucket.org/geertj/winpexpect/wiki/Home


来源:https://stackoverflow.com/questions/11320639/run-an-executable-using-wexpect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!