Python how to read output from pexpect child?

后端 未结 6 1790
攒了一身酷
攒了一身酷 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:13

    I think all you need is:

    p = pexpect.spawn('ls')
    p.expect(pexpect.EOF)
    print(p.before)
    

    or

    p = pexpect.spawn('/bin/ls')
    p.expect(pexpect.EOF)
    print(p.before)
    

    or

    p = pexpect.spawn('/bin/bash -c "ls"')
    p.expect(pexpect.EOF)
    print(p.before)
    

    or even

    print(pexpect.run('ls'))
    

提交回复
热议问题