[英]Pipe Output to Python File
How do a make a python script take input that has been piped to it. 使python脚本如何接受已通过管道传递给它的输入。 Is that a
sys.argv
moment. 那是
sys.argv
时刻。 To be clear I want to figure out how to code the python side to receive input like this: 为了清楚起见,我想弄清楚如何对python端进行编码以接收这样的输入:
cat someFile | domeSomething.py
Can this be done? 能做到吗? Again, to clarify, I'm not wanting to write this as passing a filename to my script and then using
open(filename)
I want to get piped input. 再次说明一下,我不想将其传递为脚本中的文件名,然后使用
open(filename)
来获取管道输入。 Thanks for the help. 谢谢您的帮助。
To get piped input, you need to read from stdin
: 要获得管道输入,您需要阅读
stdin
:
import sys
print sys.stdin.read()
stdin
is a file, so feel free to use any of the methods of a file object . stdin
是一个文件,因此可以随时使用文件对象的任何方法 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.