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.7-Write-Append—Modify

Instructions

In the modify tasks, students adapt the example code to start to create code of their own.

Make sure that the students add comments to explain what the code does.


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