Newbie: word count and Win32

Joe Christl jScPhArMistl at att.net
Tue Mar 19 15:51:22 EST 2002


On 19 Mar 2002 20:26:34 GMT, bokr at oz.net (Bengt Richter) wrote:

>
>From a quick look that[1] script, it looks like it expects to read
>from stdin only. I.e., it says
>
>    # iterate over each line on stdin
>    for line in sys.stdin.readlines() :
>        ...

I emailed the author of the post (Derrick Hudson) and that was what he told me
too.  

>
>To feed the script's stdin you'll have to redirect or pipe data to it.
>To pipe stuff on windows to a python script you have to run the interpreter
>(python) explicitly one way or another, so I would try:
>
>    python count.py < foo.txt
>
>or you could pipe the output of the windows type command, like:
>
>    type foo.txt | python count.py

The < redirect didn't seem to work from a Win32 standpoint.  I tried the:

python count.py < foo.txt 
...and...
python count.py foo.txt

... with failed results.  I forgot about the pipe |


>
>To make the script work like what you expected, it needs to look
>at the command line arguments (sys.argv) for a file name and open that,
>and use the resulting file object in place of stdin.
>

He gave a a diff output for it to accept a filename.  I am going to try that
first and report back,


>BTW, IMO you get a nicer ouput if you change the last line of the script to
>     print '%6d: %s' % (words[word], word)

I don't really care too much about the amount of times a word shows up.  I'll
try it his way and yours.  :)

>As an exercise, you could modify it to accept an option on the command line
>to output in order of frequency instead of alphbetically, and also accept
>a file name. Then look into the glob module and see if you can make it
>do multiple files from a wild card spec. Have fun.

As busy as my time is, it may be next year when I finish all that :P

>You may want to look at various scripts that were put on your disk when
>you installed, e.g. in wahtever corresponds to D:\Python22\Tools\Scripts\*
>on your system. Look at some small stuff first.
>
>Regards,
>Bengt Richter
>

Thanks,
Joe Christl



More information about the Python-list mailing list