read from standard input

Bengt Richter bokr at
Mon Aug 12 05:35:55 CEST 2002

On Fri, 9 Aug 2002 16:04:31 +0200, "Jaroslav Jákl" <j.jakl at> 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 < file, i get IOError: [Errno 9] Bad file
>Can anybody advise me?
>Jaroslav Jakl
You are apparently on windows? Try running python explicitly. The thing created by
automatic data file extension to run an app doesn't do i/o redirection properly.
E.g., this is on NT4 with python 2.2

 [20:40] C:\pywk\junk>type
 import sys
 for line in sys.stdin.readlines():
     print line

 [20:41] C:\pywk\junk>echo test.txt >test.txt

 [20:41] C:\pywk\junk> < test.txt
 Traceback (most recent call last):
   File "C:\pywk\junk\", line 2, in ?
     for line in sys.stdin.readlines():
 IOError: [Errno 9] Bad file descriptor

 [20:42] C:\pywk\junk>python < test.txt

Bengt Richter

More information about the Python-list mailing list