2

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.

betontalpfa
  • 3,454
  • 1
  • 33
  • 65
Eric Gentil
  • 261
  • 1
  • 3
  • 8
  • If you run it "by just typing c:\myfiles\evapo inputs.prj", then executable doesn't "ask for other information to run", it gets all it needs from the command line arguments. But you're not passing any arguments. Without knowing what evapo does it's impossible to be sure, but my bet is that, in the absence of any arguments, it reads stdin, and that's why it hangs. – abarnert Jul 04 '12 at 00:32

3 Answers3

4

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')
jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

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.

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

Community
  • 1
  • 1
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • @Eric Gentil: I hope this helps – pyfunc Jul 03 '12 at 23:55
  • :it seems there are two modules: wexpect and winpexpect. are they different? – Eric Gentil Jul 04 '12 at 00:28
  • @EricGentil: Yes. Both seems to be a port of pexpect. Also If your program expect filename as command line argument then my solution is not valid. I was under impression that this programs requires user input like typing password. – pyfunc Jul 04 '12 at 00:33
0

I was able to make it work. You need to download wexpect from github and follow the instruction on wiki on how to build it.

https://github.com/raczben/wexpect/wiki/Wexpect-with-pyinstaller#how-to-use-wexpect-with-pyinstaller

once you are done with building of wexpect, to create one exe click the link below and follow the instruction

https://github.com/raczben/wexpect/issues/12#issuecomment-605390122