4

Possible Duplicate:
How do you read from stdin in python

Hi All,
I am a newbie in python. I was wondering how I could manage to get data from standard input, such as getline in c++ and similar, so that from terminal/console I can use a text file as an input for a python script.

Thanks

Community
  • 1
  • 1
Bob
  • 10,159
  • 24
  • 84
  • 136

1 Answers1

5

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
Blender
  • 275,078
  • 51
  • 420
  • 480