Piping data into script under Win32

R

runes

I trying to figure out a way to make a python script accept data output
from another process under Windows XP, but I fail miserably. I have a
vague memory having read this is not possible with Python under
Windows...

But googling for a clue I came across this from /Learning Python/ (Lutz
& Ascher) [1]


<Learning Python>

import sys
data = sys.stdin.readlines()
print "Counted", len(data), "lines."

On Unix, you could test it by doing something like:


% cat countlines.py | python countlines.py
Counted 3 lines.

On Windows or DOS, you'd do:


C:\> type countlines.py | python countlines.py
Counted 3 lines.

</Learning Python>


So: Is it possible to pipe data under DOS/Win9x but not NT/2k/XP or is
it a bug in /Learning Python/ ?


I've tried about all kinds of sane and insane approaches involving
sys.stdin, but they all results in IOError: [Errno 9] Bad file
descriptor.


[1] <http://www.oreilly.com/catalog/lpython/chapter/ch09.html>
 
J

Jaime Wyant

I can't think of any reason that wouldn't work. You've entered the
code in exactly like you have it below?

Could you paste the traceback from your console?

jw
 
R

R. C. James Harlow

I trying to figure out a way to make a python script accept data output
from another process under Windows XP, but I fail miserably. I have a
vague memory having read this is not possible with Python under
Windows...

C:\> type countlines.py | python countlines.py
Counted 3 lines.

Are you definitely doing this and not:

C:\> type countlines.py | countlines.py

That will give you the error you're seeing. The example you posted works for
me.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBCYHpwY6W16wIJgxQRArR9AJ4vRbHQELs6d6BqUUDDNGBxAhWMhACgh4eR
Sg8+vSF93a4RR7fgFcRNJDE=
=3j2L
-----END PGP SIGNATURE-----
 
R

runes

You being so sure about what you were saying, was what I needed.
Thanks!

Under Windows, I'm used to rely on the PATHEXT env. variable, so I
typically don't write "python scriptname.py args", but just "scriptname
args".

So:
type countlines.py | python countlines.py = Success
type countlines.py | countlines.py = Failure

Why doesn't the latter work?

Traceback:
F:\groks>type countlines.py | countlines.py
The process tried to write to a nonexistent pipe.
Traceback (most recent call last):
File "C:\groks\countlines.py", line 2, in ?
data = sys.stdin.readlines()
IOError: [Errno 9] Bad file descriptor
 
R

R. C. James Harlow

type countlines.py | python countlines.py = Success
type countlines.py | countlines.py = Failure

Why doesn't the latter work?

Don't quote me on this, but I think it's because invoking countlines.py
involves running some sort of wrapper that discards its stdin and stdout.

If anyone has a more authorative answer, I'd like to know, because this caught
me out too.

james.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBCYIVHY6W16wIJgxQRAjScAJ4umerCch3+aC8ORhJzNk3i7FHrwQCfcAxl
/99yvz5++XsF5qf06rhdZic=
=fAya
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top