Dec-29-2016, 04:39 PM
I'm using Python 3.5.2 on Linux.
I'm trying to get my program to take input from a pipe (i.e.
If there is a way to accept input from both a pipe and the terminal without changing the value of sys.stdin, I'm open to suggestions.
Apologies for the URLs, but I can't post clickable links yet.
I'm trying to get my program to take input from a pipe (i.e.
find ~/docs | main.py
) and later take input from the terminal. After reading this Stack Overflow post (stackoverflow.com/questions/7141331/pipe-input-to-python-program-and-later-get-input-from-user), I tried opening /dev/tty to replace sys.stdin.import sys import readline def tty_input(prompt): with open("/dev/tty") as terminal: sys.stdin = terminal user_input = input(prompt) sys.stdin = sys.__stdin__ return user_inputThe problem with this approach is that GNU readline doesn't work when sys.stdin != sys.__stdin__. I can't use the arrow keys to move the cursor or navigate the history like I can normally. I read of a patch for this issue that was proposed here (bugs.python.org/issue512981), but I don't know that anything came of it.
If there is a way to accept input from both a pipe and the terminal without changing the value of sys.stdin, I'm open to suggestions.
Apologies for the URLs, but I can't post clickable links yet.