Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python command line 'file input stream'

I'm fairly new to python coming from C/C++, I was wondering how I would get my 'main.py' to reconize/use the imput given from a bash shell as:

python main.py < text.txt

(the file is in plain text)

like image 979
Wallter Avatar asked Jun 20 '11 17:06

Wallter


1 Answers

Read from sys.stdin:

import sys
sys.stdin.read()

Being a file-like object, you can use its reading functions or simply iterate over the input lines:

for line in sys.stdin:
    print line
like image 127
Ferdinand Beyer Avatar answered Oct 15 '22 00:10

Ferdinand Beyer