Cannot Launch Interactive Program While Piping to Script in Python

后端 未结 2 645
臣服心动
臣服心动 2021-01-15 06:34

I have a python script that needs to call the defined $EDITOR or $VISUAL. When the Python script is called alone, I am able to launch the $ED

2条回答
  •  感情败类
    2021-01-15 06:58

    When you pipe something to a process, the pipe is connected to that process's standard input. This means your terminal input won't be connected to the editor. Most editors also check whether their standard input is a terminal (isatty), which a pipe isn't; and if it isn't a terminal, they'll refuse to start. In the case of nano, this appears to cause it to exit with the message you included:

    % echo | nano
    Received SIGHUP or SIGTERM
    

    You'll need to provide the input to your Python script in another way, such as via a file, if you want to be able to pass its standard input to a terminal-based editor.

    Now you've clarified your question, that you don't want the Python process's stdin attached to the editor, you can modify your code as follows:

    editorprocess = subprocess.Popen([editor or "vi", temppath],
                                     stdin=open('/dev/tty', 'r'))
    

提交回复
热议问题