Python Forum

Full Version: Collecting Output from Pexpect
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using pexpect to provide login information for a long running command. That part is working. However, I then want to display the output from that command as it progresses. I am trying the following code where proc is a pexpect.spawn object:
 while True:
                try:
                        output=proc.readline(timeout=300)
                        print(output)
                except:
                        continue
While I know the command is outputting lines, the readline never returns anything, it just keeps timing out. How do I retrieve the command output as it progresses? I'm looking for the equivalent to stdout.PIPE used by popen. TIA.
Maybe you could do something like this
import sys

timeout = 300
timeoutMax = 1000
while True:
    try:
        output = proc.readline(timeout=timeout)
        while not output:
            timeout += 5
            output = proc.readline(timeout=timeout)
            if timeout == timeoutMax:
                print('Timout max reached')
                sys.exit()
    except:
        continue
Thanks but I know lines have been output before the 300 seconds expires. For some reason, which is the question, it simply is not returning or reading those lines. I'm assuming that it will return something when it detects each new line character.
I'm still looking for a solution to my problem. When I know what to expect I can get everything with proc.before and proc.after. The problem is that I only get that output when the expected data is received. What I want is to get any output generated by the command, line by line.