Python: Use input from another command

2019-06-26 08:39发布

问题:

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.

回答1:

An easy and quite versatile way to accomplish this is to use the fileinput module.

import fileinput

for line in fileinput.input()
    # do things with line

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).



回答2:

A good alternative to reading the standard input via sys.stdin.readlines() : use the pipes module.

The pipes module defines a class to abstract the concept of a pipeline — a sequence of converters from one file to another. Because the module uses /bin/sh command lines, a POSIX or compatible shell for os.system() and os.popen() is required.

Here's a nice tutorial.