Python 向 Xterm 发送命令

Python send a command to Xterm

我有一个 python 脚本可以在 emacs 中为我打开文件,为此它会像这样在 xterm 中调用一个进程

"""AutoEmacs Document"""

# imports
import sys
import os
import psutil
import subprocess
from argparse import ArgumentParser

# constants
xlaunch_config = "C:\cygwin64\home\nalis\Documents\experiments\emacs\Autoemacs\config.xlaunch"
script = "xterm -display :0 -e emacs-w32 --visit {0}"
# exception classes

# interface functions

# classes


# internal functions & classes

def xlaunch_check():
    # checks if an instance of Xlaunch is running
    xlaunch_state = []
    for p in psutil.process_iter(): #list all running process
        try:
            if p.name() == 'xlaunch.exe':# once xlaunch is found make an object
                xlaunch_state.append(p)
        except psutil.Error: # if xlaunch is not found return false
            return False
    return xlaunch_state != [] #double checks that xlaunch is running

def xlaunch_run(run):
    if run == False:
        os.startfile(xlaunch_config)
        return 0 #Launched
    else:
        return 1 #Already Running


def emacs_run(f):
    subprocess.Popen(script.format(f))
    return 0#Launched Sucessfully

def sysarg():
    f = sys.argv[1]
    il = f.split()
    l = il[0].split('\')
    return l[(len(l) - 1)]

def main():
    f = sysarg()
    xlaunch_running = xlaunch_check()
    xlaunch_run(xlaunch_running)
    emacs_run(f)
    return 0


if __name__ == '__main__':
    status = main()
    sys.exit(status)
`

它在偶尔出现错误的情况下工作得很好,但我想通过让 python 发送 Xterm 控制台使其在启动后启动命令(如“-e emacs-w32”)来使其更加通用等等基于它收到的输入。我已经尝试过这样的事情:

    # A test to send Xterm commands
import subprocess
xterm = subprocess.Popen('xterm -display :0', shell=True)
xterm.communicate('-e emacs')

但这似乎没有任何作用。除了启动终端。我已经对此事进行了一些研究,但这只会让我感到困惑。非常感谢您的帮助。

要在终端模拟器中打开 emacs,请使用:

Linux;

Popen(['xterm', '-e', 'emacs']) 

Windows:

Popen(['cmd', '/K', 'emacs'])

对于 cygwin 使用:

Popen(['mintty', '--hold', 'error', '--exec', 'emacs'])