8.14. Group Work: Reading from Files

It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home.

The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.

Learning Objectives

Students will know and be able to do the following.

Content Objectives:

Process Objectives:

8.14.1. File Paths, Names, and Extensions

A file is saved data in secondary storage on a computer. This type of storage persists even if you turn off power to the computer.

A file has a path, a name, and an extension. You have probably downloaded a file to your computer (such as TurtleEx1.py).

On a Apple computer you can find this file in the Downloads folder. The path to that file is /Users/username/Downloads/. It is the list of parent directories/folders. On a Windows computer the path would typically be C:\users\your name\downloads\. One of the things you need to be aware of when you read and write files is the path to the file.

The file name is what you have actually called the file. In this case it would be TurtleEx1.

The file extension is typically three to four characters following a . after the name. In this case the extension is py which means that it is a file with Python code in it. The txt extension is used for a plain text file.

8.14.2. Reading from Files

To read from a file, open the file which returns a file object and loop through the lines in a file. Close the file when you are done reading from it.

Data file: dogs.txt
Labrador Retriever
German Shepherd
Golden Retriever
Yorkshire Terrier

Run the code below to see what it prints.

Notice that when you run this code it adds a blank line after each line. That is because when you read a line from a file the line contains the newline character (\n). When you print a line you add another newline character which prints as a blank line.

Modify the code above to remove the newline from each line after it is read in.

You can also read all the lines from a file at once into a list. One advantage to this is that you can immediately close the file after that.

Run the code below to see what it prints.

p9-10: When would it not be best to read all of the lines in a file into a list at once?

You can also just read some of the lines from a file using the readline method. This can be handy if you just want to see what type of data is in a file, but don’t need to see the entire file.

Run the code below to see what it prints.

Modify the code above to print 4 lines and run it again.

p9-12: What do you think will happen when you try to run the code below?

Run the code below to see what it prints.


If the file that you try to open is not found, you will get an error.

Run the code below to see what it prints. When it asks for another file name type in ‘dogs.txt’.

You can use try and except to handle code that can cause exceptions.

You can also use with open(file) as name which will automatically close the file after the code block ends. It is particularly important to close the file when you write data to a file.

Run the code below to see what it prints.

You have attempted of activities on this page