Session Ready
Exercise

Functions and iteration

In this exercise, we'll define two functions, print_files() and list_files(), that will each use read_text().

The print_files() function will use a for loop to print file contents and not return anything.

The list_files() function will use a list comprehension to return a list of file contents.

In the video, we used the Path class's read_text() method to read in text files.

If you don't remember how this worked, you can take a look at the slides.

Path has already been imported from the pathlib module.

We will also use the pprint() function (short for pretty print) from the standard library pprint module to make the list output a little more readable.

Instructions
100 XP
  • In the print_files() function definition, set up the iteration instructions for the for loop.
  • Use the for loop to pass each the contents of each file to print().
  • In the list_files() function definition, use the read_text() method to read file contents.
  • Set up the list comprehension to pass each filename to Path.