python standard input [duplicate]
In Python, how can I get data from standard input? I want to be able to specify a text file as an input to the sc开发者_运维技巧ript from the command line.
You can use sys.stdin
. It acts like a file object, so treat it as one when you use it:
#!/usr/bin/env python
import sys
print sys.stdin.read()
Running this by itself hangs the interpreter, but adding a pipe operator does what it should:
> echo "asd" | python test.py
> asd
精彩评论