"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 |
|
Given one or more valid files, it should print the number of lines, words, and characters, each in columns 8 characters wide, followed by a space and then the name of the file. Here’s what it looks like for one file:
$ ./wc.py ../inputs/scarlet.txt 7035 68061 396320 ../inputs/scarlet.txt
When there are many files, print the counts for each file and then print a "total" line summing each column:
$ ./wc.py ../inputs/s*.txt 7035 68061 396320 ../inputs/scarlet.txt 17 118 661 ../inputs/sonnet-29.txt 3 7 45 ../inputs/spiders.txt 7055 68186 397026 total