A Word Count Program. This code should be written in python 3 and Include python
ID: 3846559 • Letter: A
Question
A Word Count Program. This code should be written in python 3 and Include python comment for each code.
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.
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.
Here is a sample run of the program.
If you use the sample file sonnet18.txt provided, the output should look like this:
This output should be printed to the given output file, not the screen.
Explanation / Answer
import string
import sys
def main():
print ("Program determines the number of lines, words in a file.")
file_name = input("Input Text file ")
output_name= input("Output Text file")
chars = words = lines = 0
with open(file_name, 'r') as in_file:
for line in in_file:
lines += 1
words += len(line.split())
sys.stdout = open (output_name,"w")
print ("Words=",words)
print ("lines=",lines)
sys.stdout.close()
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.