"I love to count!" — Count von Count
| Counting things is a surprisingly important programming skill. Maybe you’re trying to find how many pizzas were sold each quarter or how many times you see certain words in a set of documents. Usually the data we deal with in computing comes to us in files, so we’re going to push a little further into reading files and manipulating strings by writing a Python version of the venerable Unix |
|
We’re going to write a program called wc.py that will count the lines, words, and bytes found in each input. The counts will appear in columns 8 characters wide and will be followed by the name of the file. The inputs for the program which may be given as one or more positional arguments. For instance, here is what it should print for one file:
$ ./wc.py ../inputs/scarlet.txt
7035 68061 396320 ../inputs/scarlet.txt
When counting multiple files, there will be an additional "total" line summing each column:
$ ./wc.py ../inputs/const.txt ../inputs/sonnet-29.txt
865 7620 44841 ../inputs/const.txt
17 118 661 ../inputs/sonnet-29.txt
882 7738 45502 total