Linux 如何通过python中的shell管道接收参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6106437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to receive arguments via shell pipe in python?
提问by Woltan
I would like to do something like this:
我想做这样的事情:
find -name "foo*" | python main.py
and access all the files that were found by the find program. How do I access that in Python?
并访问 find 程序找到的所有文件。我如何在 Python 中访问它?
采纳答案by Andreas Jung
import sys
for line in sys.stdin:
print line
回答by David Claridge
Use sys.stdin.read() or raw_input()
使用 sys.stdin.read() 或 raw_input()
A pipeline just changes the stdin file descriptor to point to the pipeline that the command on the left is writing to.
管道只是将 stdin 文件描述符更改为指向左侧命令正在写入的管道。
回答by manojlds
I think you can do:
我认为你可以这样做:
import sys
print sys.stdin.readlines() #or what you want