By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Piping data into script under Win32

P: n/a
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>

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

On 15 Apr 2005 19:11:44 -0700, runes <ru*********@gmail.com> wrote:
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>

--
http://mail.python.org/mailman/listinfo/python-list

Jul 19 '05 #2

P: n/a
On Saturday 16 April 2005 03:11, runes wrote:
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)

iD8DBQBCYHpwY6W16wIJgxQRArR9AJ4vRbHQELs6d6BqUUDDNG BxAhWMhACgh4eR
Sg8+vSF93a4RR7fgFcRNJDE=
=3j2L
-----END PGP SIGNATURE-----

Jul 19 '05 #3

P: n/a
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

Jul 19 '05 #4

P: n/a
On Saturday 16 April 2005 03:43, runes wrote:
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+aC8ORhJzNk 3i7FHrwQCfcAxl
/99yvz5++XsF5qf06rhdZic=
=fAya
-----END PGP SIGNATURE-----

Jul 19 '05 #5

P: n/a
Thanks James! I'll post the explanation if I find it ;-)

Rune

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.