473,778 Members | 4,256 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading piped input in Windows

Hi,
I am writing a filter, i.e. a program that reads from stdin and writes
to stdout. It works ok when run on its own, but does not work when I try
to use another program's output.

- The producer program (t1):
print "a b c"
print "d e f"

- The filter program (t2):
import sys
r = sys.stdin.readl ines()
for i in r:
print '<',i,'>'

When i connect them using 't1 ¦ t2', I get the following error message:
Traceback (most recent call last):
File "[...]", line 2, in ?
r = sys.stdin.readl ines()
IOError: [Errno 9] Bad file descriptor

The same programs work correctly under Linux, so I suppose that Windows
handles redirectionned input differently from 'normal' console input.
Can anyone point me to a portable solution that works both under Windows
and Linux?

-PU
Jul 18 '05 #1
4 2768
Patrick Useldinger:
I am writing a filter, i.e. a program that reads from stdin and writes
to stdout. It works ok when run on its own, but does not work when I try
to use another program's output.

- The producer program (t1):
print "a b c"
print "d e f"

- The filter program (t2):
import sys
r = sys.stdin.readl ines()
for i in r:
print '<',i,'>'

When i connect them using 't1 ¦ t2', I get the following error message:
Traceback (most recent call last):
File "[...]", line 2, in ?
r = sys.stdin.readl ines()
IOError: [Errno 9] Bad file descriptor

The same programs work correctly under Linux, so I suppose that Windows
handles redirectionned input differently from 'normal' console input.
Can anyone point me to a portable solution that works both under Windows
and Linux?
It works OK for me on Windows XP SP1 with Python 2.3.2:

C:\Test\PythonT est >>t1.py | t2.py
< a b c < d e f


What versions of Windows and Python are you running?

BTW, here's a simpler way to write the loop in t2.py:

import sys
for line in sys.stdin:
print '<', line, '>'

Or this, to get rid of the extra newlines (not clear if those are intended
or not):

import sys
for line in sys.stdin:
print '<', line[:-1], '>'

-Mike
Jul 18 '05 #2
On Sat, 15 Nov 2003 18:27:22 +0100, Patrick Useldinger <p@trick.lu> wrote:
Hi,
I am writing a filter, i.e. a program that reads from stdin and writes
to stdout. It works ok when run on its own, but does not work when I try
to use another program's output.

- The producer program (t1):
print "a b c"
print "d e f"

- The filter program (t2):
import sys
r = sys.stdin.readl ines()
for i in r:
print '<',i,'>'

When i connect them using 't1 ¦ t2', I get the following error message:
Traceback (most recent call last):
File "[...]", line 2, in ?
r = sys.stdin.readl ines()
IOError: [Errno 9] Bad file descriptor

The same programs work correctly under Linux, so I suppose that Windows
handles redirectionned input differently from 'normal' console input.
Can anyone point me to a portable solution that works both under Windows
and Linux?

Some versions of windows have a bug in i/o redirection for programs started
via data/script file extension association (if you have perl on your machine
you will have the same problem piping to/from perl scripts started by extension association).
It is probable that running the interpreter directly would make it work, e.g.,

python t1.py | python t2.py

But you probably want to spell it t1|t2. On windows, t1 and t2 will have to be
executables to do that reliably across windows versions. Executables are generally
associated with the usual extensions .exe, .com, .bat, and .cmd, so one way to
solve the spelling problem is to provide a t1.bat or t1.cmd that runs python
explicitly with t1.py as arg, as you would do it from the command line, e.g.,
a t1.cmd whose single line content is

@python <absolute directory path to your script>t1.py

This will be slower than also supplying the absolute path to python, but you won't
have to change it when you upgrade and python winds up in c:\python24 instead of c:\python23.
OTOH, if you need a specific version of python to interpret your script, supplying the full
path will lock it in.

Note that a .bat or .cmd invocation of python also gives you the opportunity to pass a
command line option, e.g., python -u drive:\your\pat h\t1.py to run with unbuffered binary
stdout and stderr.

Regards,
Bengt Richter
Jul 18 '05 #3
OK, I should have googled in the first place :-(

For those who wondered, it doesn't work if you have .py extentions
associated with python.exe and just type t1 ¦ p2.

What works is

1- to have a py.cmd which calls python.exe, and then type "py t1.py ¦ py
t2.py",

2- or probably also "python t1.py ¦ python t2.py" if python.exe is in
your path (I didn't check this).

-PU
Jul 18 '05 #4
Hmmm.

t1.py | t2.py -- doesn't work, because:
t1.py >tt -- DOES work
t2.py <tt -- does NOT work. sys.stdin.filen o() shows -1 instead of 0
python t2.py <tt -- works

What's your Registry key [HKEY_CLASSES_RO OT\Python.File\ shell\open\comm and]?
Mine shows "(default)" = """C:\Apps\Pyth on\python.exe "%1" %*"""
sys.version '2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)]' sys.getwindowsv ersion() # XP

(5, 1, 2600, 2, 'Service Pack 1')

G-:
--
Georgy Pruss
E^mail: 'ZDAwMTEyMHQwMz MwQGhvdG1haWwuY 29t\n'.decode(' base64')
"Michael Geary" <Mi**@DeleteThi s.Geary.com> wrote in message news:vr******** ****@corp.super news.com...
| Patrick Useldinger:
| > I am writing a filter, i.e. a program that reads from stdin and writes
| > to stdout. It works ok when run on its own, but does not work when I try
| > to use another program's output.
| >
| > - The producer program (t1):
| > print "a b c"
| > print "d e f"
| >
| > - The filter program (t2):
| > import sys
| > r = sys.stdin.readl ines()
| > for i in r:
| > print '<',i,'>'
| >
| > When i connect them using 't1 ¦ t2', I get the following error message:
| > Traceback (most recent call last):
| > File "[...]", line 2, in ?
| > r = sys.stdin.readl ines()
| > IOError: [Errno 9] Bad file descriptor
| >
| > The same programs work correctly under Linux, so I suppose that Windows
| > handles redirectionned input differently from 'normal' console input.
| > Can anyone point me to a portable solution that works both under Windows
| > and Linux?
|
| It works OK for me on Windows XP SP1 with Python 2.3.2:
|
| C:\Test\PythonT est >>t1.py | t2.py
| < a b c
| >
| < d e f
| >
|
| What versions of Windows and Python are you running?
|
| BTW, here's a simpler way to write the loop in t2.py:
|
| import sys
| for line in sys.stdin:
| print '<', line, '>'
|
| Or this, to get rid of the extra newlines (not clear if those are intended
| or not):
|
| import sys
| for line in sys.stdin:
| print '<', line[:-1], '>'
|
| -Mike
|
|
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
10378
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
6
13673
by: chad kline | last post by:
FBSD 4.8/GCC //////// C-CODE: //////// char c; while ( 1 ) {
10
13803
by: aceto | last post by:
Hi. I'm sorry for my bad english, but it's not my motherlanguage. I need to read data from a webcam and display the images on DOS . So, how can i show this images? i have to translate the data received to a bitmap, to display it on the screen in DOS, mut i don't know how to do....can u give me some help or piece of advices? Thank you very much! Mark
6
2486
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to confirm whether he really wants to overwrite the existing output file. The problem is that the second read from stdin, to obtain the user response whether to overwrite the existing output file, never waits for the user's response. It's as if a...
7
7773
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing strReturn += input + vbCrLf input = sr.Read
6
1164
by: riquito | last post by:
I googled around, but couldn't understand how to solve this problem. I have 2 scripts # script1.py # print 'something' #script2.py x=sys.stdin.read() print 'passed'
5
32467
by: Anja | last post by:
Hi everyone, I have a question about text file reading with VBA. I want to read he whole contents of the file in one string variable. I have been able to successfully read lines using: Line Input #inputFile, inputLine ' read line into inputLine string But is there any input command that lets me read the whole file into a
7
9655
by: Adrian | last post by:
What is the best was to do this in c++. This is going to be used for unix util that should be able to have input piped to it or file name spec #include <stdio.h> int main(int argc, char *argv) { FILE *fp; int c;
6
3165
by: sapsi | last post by:
Hello, I am using HadoopStreaming using a BinaryInputStream. What this basically does is send a stream of bytes ( the java type is : private byte bytes) to my python program. I have done a test like this, while 1: x=sys.stdin.read(100) if x: print x
0
9629
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10127
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10068
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.