【问题标题】:How do I get wexpect to interact a python script?如何让 wexpect 与 python 脚本交互?
【发布时间】:2021-04-14 13:36:36
【问题描述】:

伙计们,我想知道是否存在预期的问题。 该模块是否按预期工作?

事情似乎在 linux 上按预期工作

import wexpect
child = wexpect.spawn('cmd')
child.expect('>')  #instantaneous response
child.sendline('python3')
child.expect('>')  #this stays stuck here for 15 seconds before returning with success (pexpect is instantaneous)
child.sendline('import os')
child.expect('>') #instantaneous response
child.sendline('os.curdir')
child.expect('>') #instantaneous response
child.sendline()
child.expect('>') #raises TIMEOUT
child.before() 
# Traceback (most recent call last):
#File "<stdin>", line 1, in <module>
#TypeError: 'str' object is not callable
#contrast this to pexpect: b">>> os.curdir\r\n'.'\r\n>>> os.curdir\r\n'.'\r\n>>> "

【问题讨论】:

    标签: python-3.x pexpect wexpect


    【解决方案1】:

    我发现我可以直接使用 pexpect: pexpect windows support starting version 4.0

    import pexpect
    from pexpect.popen_spawn import PopenSpawn
    child = pexpect.popen_spawn.PopenSpawn('cmd')
    child.sendline('py -3 -i')
    child.expect('>{2,}')
    child.before #gives the string before the match
    child.after #gives the match string
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2016-05-29
      • 1970-01-01
      • 2023-04-10
      • 2016-03-03
      • 2016-12-15
      相关资源
      最近更新 更多