python, set terminal type in pexpect

时光怂恿深爱的人放手 提交于 2019-11-29 11:09:54
Rickard Lindroth

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!