如何解决如何让 wexpect 与 python 脚本交互?
伙计们,我想知道 w/wexpect 是否存在问题。 此模块是否按预期工作?
在 linux pexpect 中,事情似乎按预期工作
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>>> "
解决方法
我发现我可以直接使用 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。