7 Words Count: Reading files/STDIN, iterating lists, formatting strings

 

"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 wc (word count) program. We’re going to write a program called wc.py that will count the characters, words, and lines for all the files given as positional arguments.

the count

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

7.1  Writing wc.py

7.1.1  Defining file inputs

7.1.2  Iterating lists

7.1.3  What you’re counting

7.1.4  Formatting your results

7.2  Solution

7.3  Discussion

7.3.1  Defining the arguments

7.3.2  Reading a file using a for loop

7.4  Review

7.5  Going Further