'How to get pexpect to enter "y" in response to input prompt

I have a command line program that I'm running with pexpect, and it outputs this line:

🧚‍♂️ Would you like to continue? [y/n]: 

Then it hangs waiting for an input.

I want to enter "y" using pexpect and have the program continue.

I've tried

pexpect.run("[COMMAND]", events={"[y/n]: ": "y"})

and various combinations of the string to capture, including \[y/n\], as well as various combinations of the string to input, including y\n.

It appears to not work because the program doesn’t continue, but I don’t see an error message because it’s suppressed by pexpect, I think.

I'm not sure if it makes a difference, but the cursor begins one space after the colon.

How can I get this to work?


EDIT:

With this code suggested by an answer, I'm able to see the error output:

child = pexpect.spawn('command') 
child.expect('[y/n]:') 
child.sendline('y')

I'm getting pexpect.exceptions.TIMEOUT: Timeout exceeded. because it looks like the regex isn't able to match.

buffer (last 100 chars): b'rty-1643399100\r\n\x1b[0m\xf0\x9f\x93\x8c Would you like to continue? [y/n]:\x1b[0m '
before (last 100 chars): b'rty-1643399100\r\n\x1b[0m\xf0\x9f\x93\x8c Would you like to continue? [y/n]:\x1b[0m '
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 43897
child_fd: 7
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: re.compile(b'[y/n]:')

This helped me troubleshoot and figure out I needed \[y/n\]:. Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source