Python: Use input from another command

馋奶兔 提交于 2020-01-14 10:39:09

问题


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.



来源:https://stackoverflow.com/questions/9708484/python-use-input-from-another-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!