Python Forum
Not able to read the text using pexpect/expect
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not able to read the text using pexpect/expect
#1
Hi,
I am new to python i am trying some automation where in i want to call a terminal application from python and control the same.
I have tried Pexpect where i am able to open the application, but not able to read the contents from the terminal.
pasted below is my code:

import pexpect

child = pexpect.spawn ('./test_app -g',timeout=3,encoding='utf-8')
#i am expecting the below string
return_val=child.expect(['Enable QSE API? (Y/n)'])
#if i receive the same string then i want to pass y to the terminal
child.sendline('y')

#now i am expecting the below string
child.expect('Enable log? (Y/n)')
child.sendline("y")
When i execute the above script always timeout occurs and gets the below erorr/log message:

Error:
user@user-laptop:~/Automation$ python3 script_v1.py Traceback (most recent call last): File "script_v1.py", line 5, in <module> return_val=child.expect(['Enable QSE API? (Y/n)']) File "/usr/local/lib/python3.6/site-packages/pexpect/spawnbase.py", line 344, in expect timeout, searchwindowsize, async_) File "/usr/local/lib/python3.6/site-packages/pexpect/spawnbase.py", line 372, in expect_list return exp.expect_loop(timeout) File "/usr/local/lib/python3.6/site-packages/pexpect/expect.py", line 181, in expect_loop return self.timeout(e) File "/usr/local/lib/python3.6/site-packages/pexpect/expect.py", line 144, in timeout raise exc pexpect.exceptions.TIMEOUT: Timeout exceeded. <pexpect.pty_spawn.spawn object at 0x7fae358ece10> command: ./test_app args: [b'./test_app', b'-g'] buffer (last 100 chars): '\x1b[2J\x1b[1dEnable QSE API? (Y/n) ' before (last 100 chars): '\x1b[2J\x1b[1dEnable QSE API? (Y/n) ' after: <class 'pexpect.exceptions.TIMEOUT'> match: None match_index: None exitstatus: None flag_eof: False pid: 7737 child_fd: 5 closed: False timeout: 3 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('Enable QSE API? (Y/n)') user@user-laptop:~/Automation$
Something about the application: It runs on Ubuntu 18.x.x. terminal, once its started it will publish a menu and the user have to input the menu options.
Note: When i am tying to run the application manually, everything works fine.


Looking forward for some guidance from experts.


Regards
Johnson
Larz60+ write Jan-06-2022, 11:04 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply


Messages In This Thread
Not able to read the text using pexpect/expect - by Bipinjohnson - Jan-06-2022, 04:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and storing a line of output from pexpect child eagerissac 1 4,329 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Read text file, modify it then write back Pavel_47 5 1,696 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  read a text file, find all integers, append to list oldtrafford 12 3,740 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Use pexpect to send user input alisha17 0 1,941 May-10-2022, 02:44 AM
Last Post: alisha17
  Sudden Problem with pexpect gw1500se 3 2,461 Nov-19-2021, 11:21 PM
Last Post: bowlofred
  How to use pexpect in python? tiho_bg 1 1,562 Oct-30-2021, 02:50 PM
Last Post: Yoriz
  [SOLVED] Read text file from some point till EOF? Winfried 1 2,002 Oct-10-2021, 10:29 PM
Last Post: Winfried
  Open and read multiple text files and match words kozaizsvemira 3 6,805 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  Pexpect timesout before executing whole output eagerissac 0 1,537 Jun-23-2021, 03:30 AM
Last Post: eagerissac
  pexpect startup help korenron 2 3,529 Apr-27-2021, 07:23 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020