Lab 12: Find the most frequent words in a text read from a file
Write a Python program to compute the most frequently used words from the text file.
Example
Pre-Lab Questions
Distinguish between syntax errors (compile time) and exceptions (run time).
There may not exist a file with the user provided filename(s) on the command line. In that case, a built-in OS-level exception will be
raise
d when the program is executed. Write a Python functionopen_file(filename)
that handles the appropriate exception (https://docs.python.org/3/library/exceptions.html#FileNotFoundError) and re-raise
the exception (allowing a caller to handle the exception as well). To help you get started, the basic code foropen_file
is already provided below.When such a function
open_file()
is called with the appropriate argument, it is recommended coding practice to have exception handling code in the calling code or function to handleOS_exceptions
. What would that exception handling code be for handling the specific exception which was raised in the previous question?What is the difference between a text file and a binary file?
Imagine that the user specifies a filename, and a file exists with the same name but it happens to be a binary file and not a text file. If the program tries to read a binary file when it is expecting a text file, other exceptions will occur. What additional exception handling code would you add to handle all other types of exceptions that may arise, including the binary file scenario?
Post-lab Questions
How can you compare whether two files are equal in Python?
Related Material
http://programmingzen.com/use-python-to-detect-the-most-frequent-words-in-a-file/
http://mcsp.wartburg.edu/zelle/python/sigcse-workshop/mgp00047.html
Last updated