replit

Before you start this course, you must login to Replit

If you have forgotten your Replit account user name or password, ask your Coder Coach.

CoderSports


14.1-Read-Predict-Run

Instructions

At the ‘predict’ & ‘run’ stages students work entirely with example code. They should inspect it carefully and write a prediction about what it will do.

They then run the code and compare the result to their prediction.


Learning Objectives/Goals

Concept Explanation Code Example
Read Python can only read contents of the file. It cannot modify any information in the file. r my_file = open("test.txt", "r")
Write When python opens the file, it deletes everything inside. Then it can write new information to the file. All old information is lost. w my_file = open("test.txt", "w")
Append Python can add text to the end of the file. This keeps all old information while allowing new information to be added. a my_file = open("test.txt", "a")
Close When done working with a file, the file must be closed. close() my_file.close()

In this unit you will learn about

Files can be opened in either read, write, or append mode.

When working with files, the file is opened and stored in a variable.

my_file = open("test.txt", "r")

Then, once done with the file, it must be closed.

my_file.close()

Help!! My code doesen’t work!

  • Not including the file extension (.txt)
  • Consistency between file variable used with open and in the loop.
  • Wrong case in filename. (my_file vs my_File)
  • Not indenting inside loops.
  • Not closing file once task is completed.

Hints