import sys # # Open the input and output files # if (len(sys.argv) == 1): inf = sys.stdin outf = sys.stdout elif (len(sys.argv) == 2): try: inf = open(sys.argv[1], "r") except IOError: print "Error opening",sys.argv[1]," (file might not have been found)" sys.exit(-1); outf = sys.stdout elif (len(sys.argv) == 3): try: inf = open(sys.argv[1], "r") except IOError: print "Error opening",sys.argv[1]," (file might not have been found)" sys.exit(-1) outf = open(sys.argv[2], "w") else: print "Usage:",sys.argv[0]," " sys.exit(-1) # # Initialize totals to 0 # line_count = 0 word_count = 0 char_count = 0 line = inf.readline() while (line != ""): line_count = line_count + 1 char_count = char_count + len(line) - 1 # # Count the words # line = line.rstrip() words = line.split(" ") for w in words: if w != "": word_count = word_count + 1 line = inf.readline() # # Close the input file # inf.close() # # Write the results to the output file and close it # outf.write("Lines: " + str(line_count) + "\r\n") outf.write("Words: " + str(word_count) + "\r\n") outf.write("Chars: " + str(char_count) + "\r\n") outf.close()