简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM