Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Python function named word_count that takes one str parameter: the input

ID: 3571995 • Letter: W

Question

Write a Python function named word_count that takes one str parameter: the input filename. The function shall open the input file, read its contents, and print out the line count, word count, character count, and input filename (to standard output). This same text should be written into a log file named word_count.log. Each call to the word_count function adds a single line to word_count.log. (For extra credit, only open the file once per function call.) For example, if the input file is seuss.txt, the printed line should be: 5 21 89 seuss.txt.  

seuss.txt (the newline character immediately follows the last letter in each line o

ne fish two fish

red fish blue fish

I am Sam, Sam I am.

Do you like green eggs and ham?

-update the module to include a testing “main” function that calls your function. For the input file, use seuss.txt. The testing main should run when the module itself is run, but not when the module is imported.

Explanation / Answer

Please find the required program along with its output. Please see the comments against each line to understand the step.

def word_count(fname):
num_lines = 0 #initialize lines,words and chars count as 0
num_words = 0
num_chars = 0

with open(fname, 'r') as f: #open inputfile in read mode
for line in f: #for each line in the file
words = line.split() #split the current line to get the words

num_lines += 1 #increment the no:of lines
num_words += len(words) #add the words in the lines to the num_words
num_chars += len(line) #add no:of chars in the line to the num_chars
printLine = "{0} {1} {2} {3} ".format(num_lines,num_words,num_chars,fname) #create a string with the output statement
print(printLine) #print the output string
f=open("word_count.log", "a+") #open the output log file
f.write(printLine) #write to output file
  
  
word_count("seuss.txt")

------------------------------------------------------------

OUTPUT:

sh-4.3$ cat seuss.txt                                                                                                                                                                                         

Hi Hello                                                                                                                                                                                                      

How are you?                                                                                                                                                                                                  

Thanks                                                                                                                                                                                                        

sh-4.3$ python3 main.py                                                                                                                                                                                       

3 6 29 seuss.txt                                                                                                                                                                                              

                                                                                                                                                                                                              

sh-4.3$ cat word_count.log                                                                                                                                                                                    

3 6 29 seuss.txt                                                                                                                                                                                              

sh-4.3$

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote