Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE
Responsive image

Lesson Video


Lesson Tasks

  • Watch the Lesson video
    Make notes on your notes sheet.
  • Read the lesson notes
    Make notes on your notes sheet.
  • Complete the learning activities
    Make any notes on your notes sheet.
  • Check the Learning outcomes
    Update your learning objectives

What do I need to Learn?

I need to learn how to read data from a text file.
I need to learn how to write data to a text file.

Key Terms

RAM Voliatile Non-volatile Secondary device File handle Read Write Append

File Handling - Learning Activities

Reading a file

To open a file in python we use the open() function.
We put the name of the file inside of speach marks "demo.txt" The "r" means open the file for reading only.
We then assign the file to the myFile variable, which is an object.
We then use the read() method for reading the contents of the file:

To read all of the file we can loop through each line like this:

Writing to a file

We open the file "demo.txt" and append, "a", the text to the file using write:

We open the file "demo.txt" and overwrite the content "w" :

Coding activities

1. Before starting the coding task you must click on the Python for Beginners link under the notes and read through.

2. Click on the button below and complete as many exercises as you can

Python exercises

Learning Outcomes

I can copy code to read from or write to a text file.

I can create my own text file and read and write to it.

I can use a text file in my own program to read from and write data to.