0

伙计们,我想知道是否有问题 w/wexpect。该模块是否按预期工作?

事情似乎在 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>>> "
4

1 回答 1

0

我发现我可以直接使用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
于 2021-01-09T14:19:18.743 回答