Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

pexpect - How do I get wexpect to interact a python script?

Folks, I would like to know if there is an issue w/ wexpect. Is this module working as expected?

things seem to work as expected in pexpect on 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
'.'
>>> os.curdir
'.'
>>> "

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found that I could just use pexpect directly: 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...