开发者

python standard input [duplicate]

开发者 https://www.devze.com 2023-02-20 09:03 出处:网络
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 f
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

精彩评论

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