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

Lesson Video

26

Lesson Tasks

  • Watch the Lesson video
    Make notes if needed.
  • Open your Learning Journal
    Complete Task 1 in your Learning Journal
  • Complete the learning activities
    Make sure you complete the book tasks in your Unit Booklet
  • Complete End of Task Assessment
    Update your learning objectives

What do I need to Learn?

0 results forGuest0 results

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.