I'm on WindowsXP. This seems to affect any process, but I'll use Python3.2 to demonstrate it. A script, 'filter.py':
import sys
for line in sys.stdin:
print(line)
Running it like this:
echo hello | filter.py
Breaks like this:
Traceback (most recent call last):
File "F:\Documents and Settings\jhartley\docs\projects\filtercwd\filter.py", line 3, in <module>
for line in sys.stdin:
TypeError: 'NoneType' object is not iterable
Sure enough, adding a print to discover the value of sys.stdin reports that it is None (and of NoneType.)
Alternatively, running it like this:
echo hello | python filter.py
(with an explicit invokation of python) works perfectly fine.
My .py files are hooked up to execute using Python using the assoc and ftype mechanism (the way Windows associates particular filename extensions to be executed using particular programs):
> assoc .py
.py=Python.File
> ftype Python.File
Python.File="F:\Python32\python.exe" "%1" %*
(this is the same 'python.exe' that is foremost on my path)
Update: This isn't a Python thing. The same thing 开发者_C百科happens if I create filter.sh, run using cygwin bash. Explicitly running 'echo hello | bash filter.sh' works fine, but 'echo hello | filter.sh' which executes filter.sh using bash via the assoc and ftype mechanism, fails with '/dev/stdin: No such file or directory'.
So I do I have to add this explicit 'python' to all my command lines? Plus, I'm curious about why it's breaking. Is it just some peculiarity of my machine, or do other people see this too?
What echo
are you using? Do you have a UNIX-style echo
utility on your path or are you just using the Microsoft one (which does not do what you want it to)? What happens if you type echo on
at the command prompt?
I am running in Windows 7 and was unable to reproduce this. I have a UNIX-style echo
utility on my path. I have installed ruby 1.9.2 and written tiny program in test.rb
:
while s = $stdin.gets
print s
end
The program runs when I type any of these commands:
test.rb
ruby test.rb
echo hello | test.rb
echo hello | ruby test.rb
Commands 1 and 2 behave identically. Commands 3 and 4 behave identically. Everything behaves as I would expect.
EDIT 1: Here are some commands I ran:
C:\Users\David\Documents\scraps\test_ruby>ls
test.rb
C:\Users\David\Documents\scraps\test_ruby>assoc .rb
File association not found for extension .rb
C:\Users\David\Documents\scraps\test_ruby>echo hi | test.rb
hi
I don't know much about ftype, but maybe you should install ruby on your system and see if my example program works for you.
精彩评论