# Class notes for Wednesday, February 8, 2006

# This program analyzes a text file specified by the user and
# reports the total number of characters, words, and lines in
# the file.  It illustrates basic file processing in Python.

def filecount():

    # get the file name from the user
    filename = raw_input("Enter file name: ")

    # open the file, read in all of the lines from the file and
    # store them as a list of strings, and then close the file
    f = open(filename)
    lines = f.readlines()
    f.close()

    # go through the list of strings, adding up the total number
    # of characters, words, and lines
    numChars, numWords, numLines = 0, 0, 0
    for line in lines:
        numChars = numChars + len(line)
        numWords = numWords + len(string.split(line))
        numLines = numLines + 1

    # a backslash \ at the end of a line indicates that it continues
    # on to the next line
    print "%s contains %d characters, %d words, and %d lines" % \
          (filename, numChars, numWords, numLines)

