In bash, you can do echo a | python -c "print('example:', input())"
and you will get an output of example: a
. How can you do the same thing in python.
I have tried doing this:
pid = os.fork()
if not pid:
os.dup2(0, 1)
os.execv("/usr/local/bin/python3", ["/usr/local/bin/python3", "-c", "print('example:', input())"])
else:
subprocess.call(["echo", "a"])
and I have tried this:
proc1 = subprocess.Popen(["echo", "a"], stdout=subprocess.PIPE)
proc2 = subprocess.Popen(["python", "-c", "print('example:', input())"], stdin=subprocess.PIPE)
proc2.stdin.write(proc1.stdout.read())
but both times this error was raised:
Traceback (most recent call last):
File "<string>", line 1, in <module>
EOFError