what does "lost sys.stdin" error mean?

L

lynvie

I have a very simple program from the first chapter of a book on
python 3 (I'm a novice). I called the program tmp.py and the data
input file is sum.dat (just a list of numbers, 1 per line). When I
type into my command shell "tmp.py < sum.dat" I get an error from
"line=input()" -- that's line 7 from the code below. The exact error
message is:
RuntimeError: input(): lost sys.stdin.

print("Type integers, each followed by Enter; or ^D or ^Z to finish")
total=0
count=0

while True:
try:
line=input()
if line:
number=int(line)
total += number
count += 1
print("number =",number)
except ValueError as err:
print(err)
continue
except EOFError:
break

if count:
print("count =", count, "total =", total, "mean =", total/
count)
 
T

Tim Harig

I have a very simple program from the first chapter of a book on
python 3 (I'm a novice). I called the program tmp.py and the data
input file is sum.dat (just a list of numbers, 1 per line). When I
type into my command shell "tmp.py < sum.dat" I get an error from

What OS are you using? I know that pipes cause issues when working with
interpreters on Windows. You must call the python interpreter directly or
it will not be able to handle the pipe directly. I don't remember exacly
how the issue manifests but I remember it has bitten me before on Win32. I
don't currenlty have a Windows machine to test on.
"line=input()" -- that's line 7 from the code below. The exact error
message is:
RuntimeError: input(): lost sys.stdin.

I am working on Linux and I have been unable to produce your error. My
Python version identifies itself as:
'3.0.1 (r301:69556, Mar 17 2009, 11:42:03) \n[GCC 4.1.2]'

This is what I have done using your same tmp.py file as pasted from
a shell session:

17:52,505$ count=0

17:53,506$ while [ $count -lt 20 ]; do
echo $RANDOM >> sum.dat;
newcount=`echo "$count + 1" | bc`;
count=$newcount;
done

17:53,507$ python3.0 tmp.py < sum.dat
Type integers, each followed by Enter; or ^D or ^Z to finish
number = 22657
count = 1 total = 22657 mean = 22657.0
number = 12223
count = 2 total = 34880 mean = 17440.0
number = 10250
count = 3 total = 45130 mean = 15043.3333333
number = 20919
count = 4 total = 66049 mean = 16512.25
number = 20995
count = 5 total = 87044 mean = 17408.8
number = 28988
count = 6 total = 116032 mean = 19338.6666667
number = 13015
count = 7 total = 129047 mean = 18435.2857143
number = 25701
count = 8 total = 154748 mean = 19343.5
number = 6566
count = 9 total = 161314 mean = 17923.7777778
number = 19396
count = 10 total = 180710 mean = 18071.0
number = 16771
count = 11 total = 197481 mean = 17952.8181818
number = 2039
count = 12 total = 199520 mean = 16626.6666667
number = 655
count = 13 total = 200175 mean = 15398.0769231
number = 27417
count = 14 total = 227592 mean = 16256.5714286
number = 5000
count = 15 total = 232592 mean = 15506.1333333
number = 12015
count = 16 total = 244607 mean = 15287.9375
number = 8746
count = 17 total = 253353 mean = 14903.1176471
number = 29487
count = 18 total = 282840 mean = 15713.3333333
number = 3194
count = 19 total = 286034 mean = 15054.4210526
number = 8225
count = 20 total = 294259 mean = 14712.95

As you can see, it seems to be working as prescribed.
 
L

lynvie

Thanks for the additional info! I had no idea this was an old Windows
problem... I'm using Windows XP and I'm up-to-date on all my patches.

The good news is I can invoke the script correctly now, thanks to your
help!
 
T

Tim Harig

Thanks for the additional info! I had no idea this was an old Windows
problem... I'm using Windows XP and I'm up-to-date on all my patches.

s/problem/feature/
Steve Ballmer, CEO of Microsoft, would prefer that you think of it as a
'feature'.

This is a problem that I would suspect has plagued a lot of people because
it doesn't seem to be overly documented in any of the popular python
learning materials. Even Mark Hammond & Andy Robinson's book _Python
Programming on Win32_ doesn't mention the problem directly in its Windowes
gotchas section-- though it does mention there are a number of bugs in the
Windows implementation of popen().
The good news is I can invoke the script correctly now, thanks to your
help!

Happy Pythoning!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top