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
Get step-by-step solutions from verified subject matter experts
