When calling a Python script via
echo foobar | python myscript.py <<< test123
How can I get both strings (the "foobar" and "test123") in the Python script?
When trying
import sys
import select
r, w, e = select.select([sys.stdin], [], [], 0)
if r:
line = sys.stdin.readline().strip()
print("Line: " + line)
the script only returns "test123" if called as echo foobar | python myscript.py <<< test123.
If called via echo foobar | python myscript.py the "foobar" is returned.
If called via python myscript.py <<< test123 also "test123" is returned.
But how can I get both strings?
It's not possible since both pipe and herestring use stdin, so one will always take precedence (the herestring).
As a workaround, you can use process substitution
<(). The script then needs to take a filename as an argument, e.g.:If you want to simplify, you can use
fileinput:(stdin is specified as
-.)