Python how to read output from pexpect child?

后端 未结 6 1804
攒了一身酷
攒了一身酷 2020-12-29 22:51
child = pexpect.spawn (\'/bin/bash\')
child.sendline(\'ls\')
print(child.readline())
print child.before, child.after

All I get with this code in my

6条回答
  •  灰色年华
    2020-12-29 23:11

    copy from class spawn(SpawnBase) docstring, maybe example-2 is what you want.

    Example log input and output to a file::

    child = pexpect.spawn('some_command')
    fout = open('mylog.txt','wb')
    child.logfile = fout
    

    Example log to stdout::

    # In Python 2:
    child = pexpect.spawn('some_command')
    child.logfile = sys.stdout
    
    # In Python 3, we'll use the ``encoding`` argument to decode data
    # from the subprocess and handle it as unicode:
    child = pexpect.spawn('some_command', encoding='utf-8')
    child.logfile = sys.stdout
    

提交回复
热议问题