sys.stdin on windows

Z

zugnush

I often grep particular patterns out of large logfiles and then
pipeline the output to sort and uniq -c
I thought today to knock up a script to do the counting in a python
dict.

This seems work in linux

$ cat count.py
#!/usr/bin/env python
import sys
from collections import defaultdict
accumulator=defaultdict(int)
for line in sys.stdin.readlines():
accumulator[line.strip()]+=1
print "contents,count"
for key in accumulator.keys():
print key,",",accumulator[key]

$ cat test | ./count.py
contents,count
, 1
23 , 1
1 , 1
3 , 2
2 , 2
5 , 3

When I try to run the same thing on windows I get
IOError: [Error 9] Bad file descriptor

How can I make this more windows friendly?

Thanks
Neil
 
G

Gabriel Genellina

I often grep particular patterns out of large logfiles and then
pipeline the output to sort and uniq -c
I thought today to knock up a script to do the counting in a python
dict.

This seems work in linux

$ cat count.py
#!/usr/bin/env python
import sys
from collections import defaultdict
accumulator=defaultdict(int)
for line in sys.stdin.readlines():
accumulator[line.strip()]+=1
print "contents,count"
for key in accumulator.keys():
print key,",",accumulator[key]

$ cat test | ./count.py
contents,count
, 1
23 , 1
1 , 1
3 , 2
2 , 2
5 , 3

When I try to run the same thing on windows I get
IOError: [Error 9] Bad file descriptor

How can I make this more windows friendly?

Explicitely invoking the interpreter worked for me. That is, these two
commands worked fine:

type test.txt | python count.py
python count.py < test.txt

But I cannot explain *why* it doesn't work the other way.
 
T

Tim Golden

Gabriel said:
En Wed, 03 Sep 2008 06:16:03 -0300, (e-mail address removed)
I often grep particular patterns out of large logfiles and then
pipeline the output to sort and uniq -c
I thought today to knock up a script to do the counting in a python
dict.

This seems work in linux

$ cat count.py
#!/usr/bin/env python
import sys
from collections import defaultdict
accumulator=defaultdict(int)
for line in sys.stdin.readlines():
accumulator[line.strip()]+=1
print "contents,count"
for key in accumulator.keys():
print key,",",accumulator[key]

$ cat test | ./count.py
contents,count
, 1
23 , 1
1 , 1
3 , 2
2 , 2
5 , 3

When I try to run the same thing on windows I get
IOError: [Error 9] Bad file descriptor

How can I make this more windows friendly?

Explicitely invoking the interpreter worked for me. That is, these two
commands worked fine:

type test.txt | python count.py
python count.py < test.txt

But I cannot explain *why* it doesn't work the other way.

Known bug in NT-based file association. I'll try
to find an online reference, but that's basically
what it comes to. I think you can faff-about with
batch files to achieve the effect, but I can't
quite remember.

http://support.microsoft.com/kb/321788

TJG
 
Z

zugnush

Gabriel said:
En Wed, 03 Sep 2008 06:16:03 -0300, (e-mail address removed)
<[email protected]> escribi :
I often grep particular patterns out of large logfiles and then
pipeline the output to sort and uniq -c
I thought today to knock up a script to do the counting in a python
dict.
This seems work in linux
$ cat count.py
#!/usr/bin/env python
import sys
from collections import defaultdict
accumulator=defaultdict(int)
for line in sys.stdin.readlines():
    accumulator[line.strip()]+=1
print "contents,count"
for key in accumulator.keys():
    print key,",",accumulator[key]
$ cat test | ./count.py
contents,count
 , 1
23 , 1
1 , 1
3 , 2
2 , 2
5 , 3
When I try to run the same thing on windows I get
IOError: [Error 9] Bad file descriptor
How can I make this more windows friendly?
Explicitely invoking the interpreter worked for me. That is, these two
commands worked fine:
type test.txt | python count.py
python count.py < test.txt
But I cannot explain *why* it doesn't work the other way.

Known bug in NT-based file association. I'll try
to find an online reference, but that's basically
what it comes to. I think you can faff-about with
batch files to achieve the effect, but I can't
quite remember.

http://support.microsoft.com/kb/321788

TJG

Thanks.

I'll ues the explicit python call.
 
G

Gabriel Genellina

Gabriel said:
En Wed, 03 Sep 2008 06:16:03 -0300, (e-mail address removed)
When I try to run the same thing on windows I get
IOError: [Error 9] Bad file descriptor

How can I make this more windows friendly?
Explicitely invoking the interpreter worked for me. That is, these two
commands worked fine:
type test.txt | python count.py
python count.py < test.txt
But I cannot explain *why* it doesn't work the other way.

Known bug in NT-based file association. I'll try to find an online
reference, but that's basically
what it comes to. I think you can faff-about with
batch files to achieve the effect, but I can't
quite remember.

http://support.microsoft.com/kb/321788

Uhmm... That KB article says the bug was corrected in Windows XP SP1, but
I have SP3 installed and the test failed. Updating the registry by hand
solved the problem. A regression maybe?
 

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,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top