2

Duplicate of this question. Vote to close.

Consider this at the windows commandline.

scriptA.py | scriptB.py

In scriptA.py:

sys.stdout.write( "hello" )

In scriptB.py:

print sys.stdin.read()

This generates the following error:

c:\> scriptA.py | scriptB.py
close failed: [Errno 22] Invalid argument
Traceback (most recent call last):
  File "c:\scriptB.py", line 20, in <module>
    print sys.stdin.read()
IOError: [Errno 9] Bad file descriptor

The "close failed" message seems to come from execution of scriptA.py.

It doesn't matter if I use sys.stdin.read(), sys.stdin.read(1), sys.stdin.readlines() etc etc.

What's wrong?

Duplicate of this question. Vote to close.

Community
  • 1
  • 1
sharkin
  • 12,162
  • 24
  • 86
  • 122
  • I just tried it with python a.py | python b.py , which works fine. See Johan's answer for the reason. – balpha Jun 29 '09 at 10:51

1 Answers1

7

It seems that stdin/stdout redirect does not work when starting from a file association. This is not specific to python, but a problem caused by win32 cmd.exe.

See: http://mail.python.org/pipermail/python-bugs-list/2004-August/024920.html

Johan
  • 3,039
  • 1
  • 20
  • 15
  • 1
    Please consider editing your response so that it states that the problem is in Windows CMD.EXE, not in Python. – John Machin Jun 29 '09 at 11:04
  • Not sure if this problem is really Windows specific, as I just encountered something similar on Linux - see my entry [Linux: Pipe into Python (ncurses) script, stdin and termios - Stack Overflow](http://stackoverflow.com/questions/3999114/linux-pipe-into-python-ncurses-script-stdin-and-termios) – sdaau Oct 22 '10 at 17:00
  • Thanks a bunch! I thought I was going nuts – Konrads Sep 30 '12 at 08:43