Skip to content Skip to sidebar Skip to footer

Python3.6 Pexpect Is Not Writing Logs To A Logfile

I am trying to log pexpect logs to a file. The code was working in python2.7 but logs not getting printed in python3.6 import pexpect child = pexpect.spawn('telnet IP') fout = open

Solution 1:

It's a bit hard to believe that exactly this code was working in Python 2.7 ;)

Your contextmanager exits right after child.logfile = fout completes, hence your file handle is closed when your child process tries to write to it afterwards. You'll have to keep the file handle open until your child finishes, e.g.:

import pexpect
withopen("abc.txt", "wb") as fout:
    child = pexpect.spawn("telnet IP")
    child.logfile = fout

# -- OR --

child = pexpect.spawn("telnet IP")
withopen("abc.txt", "wb") as fout:
    child.logfile = fout
    child.expect(<some pattern here>)

Post a Comment for "Python3.6 Pexpect Is Not Writing Logs To A Logfile"