An error occurred while reading stdin from python on Windows.

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.