开发者

python standard input [duplicate]

This question already has answers here: How do I read from stdin? (25 answers) Closed 13 days ago.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜