Python: Use input from another command

2019-06-26 08:22发布

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.

2条回答
Root(大扎)
2楼-- · 2019-06-26 08:39

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

查看更多
走好不送
3楼-- · 2019-06-26 08:49

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.

查看更多
登录 后发表回答