read from standard input

Bengt Richter bokr at oz.net
Sun Aug 11 23:35:55 EDT 2002


On Fri, 9 Aug 2002 16:04:31 +0200, "Jaroslav Jákl" <j.jakl at era.cz> 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.
>Can anybody advise me?
>thanks
>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 script.py
 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>script.py < test.txt
 Traceback (most recent call last):
   File "C:\pywk\junk\script.py", line 2, in ?
     for line in sys.stdin.readlines():
 IOError: [Errno 9] Bad file descriptor

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

Regards,
Bengt Richter



More information about the Python-list mailing list