I am wondering how can I manage input from another command from a python script.
Example:
$ cat myfile.txt | my_python_script.py
How can my script manage the input stream from the cat command ? How Can I get an input from this piped commands ?
... Thanks a lot.
An easy and quite versatile way to accomplish this is to use the
fileinput
module.This way you can both use the script in a pipeline (as you need to right now) or give one or more files to the script as a parameter (think
my_python_script.py input.txt input2.txt
).A good alternative to reading the standard input via
sys.stdin.readlines()
: use the pipes module.Here's a nice tutorial.