A common utility on word processors is a small program called “wc.” This program
ID: 3844114 • Letter: A
Question
A common utility on word processors is a small program called “wc.” This program analyzes a file to determine the number of lines and words contained in the document. Write your own version of wc. Using Python write a pseudocode that; Input: Two file names - one for the input file (infile) and one for the output file (outfile) Output: Print the total number of words and the total number of lines that were contained in the input file (infile). This output should be printed to the output file (outfile) Your program must meet the following specifications: 1. Get the input and output file names from the user. 2. Use files for all input/output. 3. Compute the number of words/lines in the input file and print the result to the output file. 4. Document the program using Python comments.
Explanation / Answer
import string def main(): print "Program determines the number of lines, words in an inputfile and writes it to outputfile." inputfile = raw_input("filepath of input file ") outputfile = raw_input("filepath of output file ") words = lines = 0 with open(inputfile, 'r') as in_file: for line in in_file: lines += 1 words += len(line.split()) f = open('outputfile', 'w') f.write("there are %d lines in inputfile and %d words"%(lines,words)) f.close() main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.