0

I have an Visual studio application which takes interactive arguments like

- PATH 

On input , MENU #1 is displayed (which again accepts arguments/user input) and again on input , MENU #2 is displayed.

I need to call this VS application (exe) from Python . I have limitation to stick to Python 2.5 version.

I tried using subprocess.popen and stdin.write.

I am able to parse through MENU#1 but unable to proceed further to MENU #2 and so on...

Any hints/examples on achieving the above.?

My code looks like:

p = subprocess.Popen('app.exe',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=None)
p.stdin.write(file_path)  # for menu 1

time.sleep(0.5)

p.stdin.write('0')         # for menu 2..

...

o,e = p.communicate()
2
  • Are you sure that the exe reads from stdin and not directly from console? Commented Aug 23, 2012 at 8:50
  • Not very sure. Janne. I pressume that the exe reads from stdin (becoz, if in case i remove p.stdin.write('0') the behavior observed is as expected.) Commented Aug 23, 2012 at 10:01

1 Answer 1

1

Use the pexpect module instead; it'll let you control a program with interactive input much better than the subprocess module can.

import pexpect

p = pexpect.spawn('app.exe')
p.sendline(file_path)
p.expect('Menu #2:.*')
p.sendline('0')

For windows, you can use wexpect.py instead, a port of the pexpect module to the Windows console.

Sign up to request clarification or add additional context in comments.

4 Comments

Dear Martijn, Many thanks for the advice. But i pressume that the pexpect module is for Linux environment. Currently i have setup running in Windows 7 / XP environment.
Have you tried it? It's a pure-python module, so there won't be any compilation issues.
Dear Martijn, Yes i tried it.>>> import pexpect Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\Python25\Lib\site-packages\pexpect.py", line 85, in <module> support it. Pexpect is intended for UNIX-like operating systems.""") ImportError: No module named resource A critical module was not found. Probably this operating system does not support it. Pexpect is intended for UNIX-like operating systems. >>>
Ah, the documentation states it'll work in a Cygwin environment, not on just Windows. Sorry about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.