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[0m\x1b[8D\x1b[K\x1b[1m\x1b[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
Ok, I found the answer. csl's answer set me on the right path.
pexpect has a "env" option which I thought I could use. like this:
a = pexpect.spawn('program', env = {"TERM": "dumb"})
But this spawns a new shell which does not work for me, our development environment depends on a lot of environmental variables :/
But if I do this before spawning a shell:
import os
os.environ["TERM"] = "dumb"
I change the current "TERM" and "dumb" does not support colours, which fixed my issue.
Couldn't find anything in the pexpect documentation for setting terminals, but you could probably start your program explicitly with a shell, and then set the terminal type there:
shell_cmd = 'ls -l | grep LOG > log_list.txt'
child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
child.expect(pexpect.EOF)
You could try something like
child = pexpect.spawn('TERM=vt100 /bin/bash', ['-c', shell_cmd])
You can also start bash with --norc and similar to avoid reading the initialization files. Check out the bash man page.
来源:https://stackoverflow.com/questions/358783/python-set-terminal-type-in-pexpect