By Vasudev Ram
Had a need recently for a simple word count program (like the UNIX wc utility), for text files on Windows, and didn't have immediate access to a ready-made tool for that (*), on the machine I was on, so wrote this simple Python program:
# wc.py # word count program import sys import os import string in_filename = sys.argv[1] print "Counting number of words in file:", in_filename try: fil_in = open(in_filename) except IOError: sys.stderr.write("ERROR: Could not open in_filename %s\n" % in_filename) sys.exit(1) word_count = 0 for lin in fil_in: words_in_line = lin.split() word_count += len(words_in_line) fil_in.close() print "File %s has %d words" % (in_filename, word_count)
You can run it with:
python wc.py text_file_name
Caveats: it's not much tested (though it did work on a few runs), and only counts words, not lines and characters (unlike the UNIX wc). I may add those later. Alternatively, that is left as an exercise for the reader (TM:)
(*) There was a recent version MS Word (Office) on the machine, but didn't feel like figuring out its crappy(ier) new interface, so wrote the tool. Go figure.
- Vasudev Ram - Dancing Bison Enterprises
No comments:
Post a Comment