I have a script which uses pexpect to start a CLI program. It works a bit like a shell where you get a prompt where you can enter some commands.
The problem I have, I think, is that this program uses a coloured prompt.
This is what I do
import pprint
import pexpect
1 a = pexpect.spawn('program')
2 a.expect("prompt>")
3 print "---------start------------"
4 print(a.before)
5 a.sendline("command")
6 a.expect("prompt>")
7 print "---------before------------"
8 pprint.pprint(a.before)
9 print "---------after------------"
10 pprint.pprint(a.after)
This is the output:
> python borken.py
---------start------------
A lot of text here from the enjoying programs start-up, lorem ipsum ...
---------before------------
' x1b[0mx1b[8Dx1b[Kx1b[1mx1b[34m'
---------after------------
'prompt>'
For some reason the first prompt colour coding borkens up things and a.before at line 8 is garbled, normal print does not work, even if I see that the command at line 5 actually produced a lot of output.
Does someone know what the problem could be, or is it possible to set the terminal type in pexpect to avoid the colours?
I am using tcsh shell
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…