Get startedGet started for free

From script to package

One common way to begin writing a package is to start with code you have already written as a script. At the time you first write this code, you may not realize how useful it might be in other places.

If you did the prerequisite course, in one exercise you wrote a script to count the number of times cats were mentioned in the book Alice in Wonderland.

In this exercise, you'll copy from that script to make a generalized function you can use on any text file for any words. This will be the first function in a new library.

Note: You can open/close the folder and files overview by clicking on the icon on the left side of the editor highlighted in red. Icon with multiple files

This exercise is part of the course

Developing Python Packages

View Course

Exercise instructions

  • Copy the code from myscript.py into the textanalysis.py file in textanalysis/ folder.
  • Modify textanalysis.py to create the function count_words(filepath, words_list) which opens the text file filepath, and returns the number of times the words in words_list appear.

Hands-on interactive exercise

Turn theory into action with one of our interactive exercises

Start Exercise