# wordcount.py # Steve Marschner (srm2) # February 25, 2014 """Demo of counting words, lines, and characters.""" import sys if __name__ == '__main__': # If the user gave us one filename on the command line, read its # contents; otherwise complain that there should have been one. if len(sys.argv) == 2: text = open(sys.argv[1]).read() # Print statistics for the string we read num_chars = len(text) num_words = len(text.split()) num_lines = len(text.split('\n')) num_stanzas = len(text.split('\n\n')) print num_chars, 'characters,', num_words, 'words,', \ num_lines, 'lines,', num_stanzas, 'stanzas.' else: print "Usage: python wordcount.py "