An error occurred while reading stdin from python on Windows.
(Jinqing's column)
A simpleCode, Read stdin, replace the output to stdout:
#! /Usr/bin/ENV Python
Import OS, sys
Input_file = SYS. stdin
Output_file = SYS. stdout
For s in input_file:
Output_file.write (S. Replace ("ABC", "def "))
The code is changed from cookbook
Recipe 2.3. Searching and replacing text in a file
Running properly on Linux:
$ Cat input.txt |./replace. py
However, when running in Windows DOS window, an error is reported:
> Type input.txt | replace. py
Traceback...
For s in input_file:
Ioerror: [errno 9] Bad file descriptor
If python is used, the call is normal:
> Type input.txt | Python replace. py
The original reason is that cmd.exe is associated with the running of the py file through the suffix.
For details, see bad pipe filedescriptor when reading from stdin in Python.
Http://stackoverflow.com/questions/1057638/bad-pipe-filedescriptor-when-reading-from-stdin-in-python
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.