Using Python subprocess.call() to launch an ncurses process

送分小仙女□ 提交于 2019-12-02 05:39:35

To run curses programs under Python I'd recommend that you use pexpect.

For example here's a simple program that starts a copy of vim, add some text, escape to command mode, issue a :w command, and then interact with the user (allowing him or her to continue editing or whatever). Then the control returns to Python:

#!/usr/bin/env python
import pexpect
child = pexpect.spawn("/usr/bin/vim")
child.send('a\n\nThis is another test.')
child.send('\x1b')
child.send(':w! test.txt\n')
child.interact()

You can also pass arguments (such as escape character, and filter functions for input and output) to the interact method. But those get a bit tricky. (On the other hand they then become your custom keyboard macro system interposed between users and the application being run under the .spawn()).

(BTW: you can send your desired sequences of keystrokes into this ct-ng dialog/menu ... it's just a matter of figuring out what those sequences need to be for your terminal settings. For example on my iTerm under MacOS X running with TERM=xterm-256color a "down arrow" cursor movement comes out as ^[[B ([Esc][Bracket][B]). That would be '\x1b[B' as a Python string literal).

Scott Dillman

After careful tracing of the execution there was a script redirecting to tee that was causing the problem.

Thanks to everyone who looked at the problem. I should have seen that in the first place; sorry for the noise.

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