Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

sys.stdin on windows

 
Jump to:  
 
zugnush@gmail.com
PostPosted: Wed Sep 03, 2008 9:16 am    Post subject: sys.stdin on windows
       
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
 

 
Gabriel Genellina
PostPosted: Wed Sep 03, 2008 9:16 am    Post subject: Re: sys.stdin on windows
       
En Wed, 03 Sep 2008 06:16:03 -0300, zugnush@gmail.com <zugnush@gmail.com>
escribi�:

Quote:
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.

--
Gabriel Genellina
 

 
Tim Golden
PostPosted: Wed Sep 03, 2008 9:16 am    Post subject: Re: sys.stdin on windows
       
Gabriel Genellina wrote:
Quote:
En Wed, 03 Sep 2008 06:16:03 -0300, zugnush@gmail.com
zugnush@gmail.com> 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.

LINK

TJG
 

 
zugnush@gmail.com
PostPosted: Wed Sep 03, 2008 10:35 am    Post subject: Re: sys.stdin on windows
       
On Sep 3, 11:16 pm, Tim Golden <m...@timgolden.me.uk> wrote:
Quote:
Gabriel Genellina wrote:
En Wed, 03 Sep 2008 06:16:03 -0300, zugn...@gmail.com
zugn...@gmail.com> 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.

LINK

TJG

Thanks.

I'll ues the explicit python call.
 

 
Gabriel Genellina
PostPosted: Wed Sep 03, 2008 11:41 pm    Post subject: Re: sys.stdin on windows
       
En Wed, 03 Sep 2008 07:16:12 -0300, Tim Golden <mail@timgolden.me.uk>
escribi�:
Quote:
Gabriel Genellina wrote:
En Wed, 03 Sep 2008 06:16:03 -0300, zugnush@gmail.com
zugnush@gmail.com> escribi�:

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.

LINK

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?

--
Gabriel Genellina
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Bmibaby HP 83 Magenta UV Value Pack coding system przedłużanie włosów Zarys międzynarodowych stosunków gospodarczych