Hemanth Deshpande
Hemanth Deshpande

Reputation: 1

Multiple stdin.write usage with python

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()

Upvotes: 0

Views: 435

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121952

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.

Upvotes: 1

Related Questions