Question: Write a function called count_words. This function is passed the name of a file, and should count the number of times each word occurs in

Write a function called count_words. This function is passed the name of a file, and should count the number of times each word occurs in the file. A dictionary should be returned, whose keys are words, and whose values are their counts. For example:

>>> freq = count_words('hamlet.txt') >>> freq['to'] 13

>>> freq['be'] 3 >>> freq['or'] 2 
>>> freq['hamlet'] 
Traceback (most recent call last): File "", line 1, in 
 freq['hamlet'] KeyError: 'hamlet' 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!