# words.py # Prints information about the words in a text file. import string def main(): fname = raw_input("Enter filename: ") infile = open(fname,'r') lineCount = 0 wordCount = 0 charCount = 0 for line in infile: lineCount = lineCount + 1; wordList= string.split(line, " ") wordCount = wordCount + len(wordList) charLineCount = 0 for word in wordList: charCount = charCount + len(word) charLineCount = charLineCount + len(word) print "Chars:", charLineCount, line, print print "The number of lines is: ", lineCount print "The number of words is: ", wordCount print "The number of characters is: ", charCount print print "The average number of words per line is: ", wordCount/float(lineCount) print "The average number of letters per word is: ", charCount/float(wordCount) main()