read from standard input

Oren Tirosh oren-py-l at hishome.net
Fri Aug 9 14:02:02 EDT 2002


On Fri, Aug 09, 2002 at 04:04:31PM +0200, Jaroslav Jákl wrote:
> Hi all,
> I want to read from stdin. I have the following script:
> 
> import sys
> for line in sys.stdin.readlines():
>     print line
> 
> and if i execute it like script.py < file, i get IOError: [Errno 9] Bad file
> descriptor.

I cannot reproduce this. Here it works just fine. Which Python version
are you using? 

BTW, if you are using Python 2.2 or higher you can use

  for line in sys.stdin:

and avoid reading the entire file into memory first.

	Oren




More information about the Python-list mailing list