Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pexpect - multiple expects

Tags:

python

pexpect

Is it possible to "wait" for different answers from an expect command at the same time?

E.g: child.expect('first', 'second')

And if YES, how can differentiate which one has triggered it?

like image 452
Kaguei Nakueka Avatar asked Dec 10 '22 18:12

Kaguei Nakueka


1 Answers

Yes, you can do it like:

i = child.expect(['first', 'second'])

The expect() method returns the index of the pattern that was matched. So in your example:

if i == 0:
    # do something with 'first' match
else: # i == 1
    # do something with 'second' match

For more: http://pexpect.readthedocs.org/en/stable/overview.html

like image 116
Quinn Avatar answered Dec 13 '22 06:12

Quinn