count the approximate number of words in the file words contents split( num_words len(wordsw print("the file filename has about str(num_wordswords " moved the file alice txt to the correct directoryso the try block will work this time at we take the string contentswhich now contains the entire text of alice in wonderland as one long stringand use the split(method to produce list of all the words in the book when we use len(on this list to examine its lengthwe get good approximation of the number of words in the original string at we print statement that reports how many words were found in the file this code is placed in the else block because it will work only if the code in the try block was executed successfully the output tells us how many words are in alice txtthe file alice txt has about words the count is little high because extra information is provided by the publisher in the text file used herebut it' good approximation of the length of alice in wonderland working with multiple files let' add more books to analyze but before we dolet' move the bulk of this program to function called count_words(by doing soit will be easier to run the analysis for multiple booksword_count py def count_words(filename)"""count the approximate number of words in file ""trywith open(filenameas f_objcontents f_obj read(except filenotfounderrormsg "sorrythe file filename does not exist print(msgelsecount approximate number of words in the file words contents split(num_words len(wordsprint("the file filename has about str(num_wordswords "filename 'alice txtcount_words(filenamemost of this code is unchanged we simply indented it and moved it into the body of count_words(it' good habit to keep comments up to date when you're modifying programso we changed the comment to docstring and reworded it slightly files and exceptions