Lesson video

In progress...


Hi, I'm Rebecca, your computing teacher for the programming part six unit.

For this lesson, you're going to need your Repl.

it account which you should have already set up with your parent or carer's permission.

You're also going to need a pen and paper to make any notes that you need to in this lesson and to try your best to remove as many distractions as possible so that you can really focus in this lesson.

Once you've got all of that ready, we can begin.

In this lesson, you will determine the purpose of external data files.

And you'll read data from an external data file.

I want you to think about this then, "How limited would gaming be if you could never save your progress?" Pause the video while you have a think about that question.

Let's take a look then.

So this is some of the things that you might have put.

So levels in games would need to be quite short.

It would be very frustrating if you had to stop knowing that you would need to start from the beginning again.

And, games will be limited in what they could offer the player as well.

So you might have had something like that.

Now a file is a term used to describe data that is held in storage on a computer.

Files come in many forms, for example, a text file, images, sound and video.

Files can often be used by other programmes for different purposes.

For example, if you create an image in a painting package, you can then use that same image in a presentation that you have made.

File handling is a term used to describe how programmes work with the files that are stored in a computer.

Having the ability to use file handling techniques in your own programmes opens up endless possibilities.

All of the programmes that you have created so far have lost any data generated within them once they have been terminated.

Saving programme data to an external file allows you to use that data again.

An example of when this might be used is to keep track of the highest score for a game.

A text file can be used to store the highest score and this can be accessed and modified each time the game is played.

Another example is when a programme can be designed to access large data sets.

A weather station can you sensors to take weather readings that are stored in a data file.

And a programme can then analyse the data in the file to make predictions about the weather.

So here's a question for you.

Can you think of any other examples of when file handling techniques might be used? Pause the video while you have a think about that.


So here's some examples.

You might have had some different ones, but your game progress, test scores, player names, restaurant orders, stock levels.

There's quite a lot of reasons why you might need to save things in an external data file.

So now we're going to look at file handling in Python.

The key file handling techniques that you need to be able to use are these ones.

So open, read, close, write and append.

Now there's some words that you might be familiar with and you might not be sure about, but we'll take you through them.

This lesson, we'll just focus on those first three, which is open, read and close.

So here we're going to look at opening and reading a text file.

In order to read the data stored in a text file, you must open it first.

Think of it a bit like a book.

You can't read what it inside if you don't open it first.

So this is how you would open a text file.

So here is an instruction that will open a text file in read mode.

So file creates an identifier for the file that is to be accessed.

Open calls a function that returns a file object.

The first argument is the name of the text file to be opened.

And then the second argument is the mode that the file will be opened.

There are four modes for opening a file.

So r is for only reading, w is for only writing, a is for adding to an existing file or appending and then r+ is for reading and writing files.

And this is important to remember, really, really important, opening a file in write mode will erase any existing file with the same name.

So be careful when you do these writing files.

Once the file has been opened, it can then be read.

So this creates a variable that will hold the contents of the text file.

And this is the identifier for the text file that has been opened.

And you can see that matches with the identifier at the beginning.

And then this is a method that is used to read the contents of the text file.

And this is important as well.

The text file must be saved in the same location as your Python file for the programme to work.

You must not use the same variable name as a file identifier in your programme as well.

That's really important 'cause it's just going to overwrite, it's not going to work.

So let's take a quick look in Repl.

it to see how text files are used within the environment 'cause it can be a little bit different depending on the development environment that you're using text files with, but this is how you do it in Repl.


So you've probably already noticed that you can actually see different files here.

We did this in part five when we had an extra Python file in there that was connected to our main Python file.

So you know that you can put extra files here that has to do with your programme.

If you wanted to just create a text file within Repl.

it, you can actually do it here.

You can also make your own text files by using a programme like Notepad, for example, on your computer, which just creates a raw text file and you save it with.

txt at the end, and that makes sure that it's a text file.

But you can do it directly in Repl.


So you just go to add file.

If I wanted to make a text file called words, then I just make sure that I put.

txt at the end of that, and then I've made a file called word.


And then I'm in the file now, so I can switch between the two and I can put some words in there.

And then I could programme some code to access this text file from the main programme.

So that's one way that you can work with text files, but you might already have your own text file that you want to upload and put inside.

So then you can click on those little three dots there, go to upload file, navigate to your text file that you want to upload and then just open it and then it will appear here as well.

Now all of the starter code that I've given you in the worksheets will already have the text files there, so you won't need to upload them and add them in.

So they should already appear there on the left-hand side for you to get started with.

So you shouldn't have to actually do that task but that's just how to do it if you did ever want to.

Now that you know how to use text files in Repl.

it, I want you to use the "Read a text file" section of your worksheet to practise reading a text file.

Off you go.

Excellent then.

So reading lines from a text file now then.

So the readline method can be used to read a single line from a text file.

The method will return the line with \n at the end of the string.

This adds a new line, and you can see that there.

Each time the readline method is called, the next line in the text file will be returned.

So you can see that it's been returned and you've got that additional line space as well.

You can also eat iterate over a text file using a for loop.

What I wanted you to do is take a look at that bit of code.

What do you think the output will be when this programme is executed without knowing really what on the text file? What do you think might happen? Let's take a look then.

So what it does, for every line in the text file, it will print the line along with a space.

So that's just a text file with, a quick fox jumped over the lazy dog, each one on its new line and it's pulled the A with the extra line space there as well.

To remove the extra line space, the strip method can be used.

So you just add that.

strip on there and it will get rid of that \n.

And that's what it would look like.

Another tool for reading lines is the readlines method You can use these to place each line of the text file as an item in a list.

So that's what it would look like.

The \n becomes more apparent when you see the data in this way.

Note that the \n is not added to the final line in the text file.

This is quite important for what's going to come later on.

If you want to populate a list with lines from a text file but you don't want \n included in the list, you can write for loop to populate the list instead.

So this is what it would look like.

When the data is copied from a text file, it would automatically be held using the string data type.

Note the quotes around each number in the list.

To change the data type, you will need to use casting.

This data should be held as an integer so you can cast it as an integer as you populate the list.

And you can see now that the quotes are no longer around each number.

Once you have performed all of the tasks that you need to with a file, it is important to close it.

This will free up system resources and protect the data.

So what I want you to do now is use the "Text File Challenge" section of your worksheet to try out some text file challenges.

Have a go, then we'll go through the solution shortly.

I want you to pause the video while you do those tasks.


Let's look at the solutions then.

So this is the solution to challenge one.

Remember, I'm going to point this out quite a lot.

There is more than one way to solve these problems, so your code might not be exactly the same as mine, but it should be similar.

And as long as it's solved the problem then it's still working.


So pause the video while you check.

So here's the next one then, text file challenge two.

This is again my version of the solution.

So yours might be slightly different.

So you can pause the video and check that with yours now.

And this is the Explorer task.

If you had to go at the Explorer task, then this would have been the solution to that.

And if you didn't have a go, it might be a good time now to take a look, pause and type this into Repl.

it and just see what would happen.

So we've covered quite a few new methods this lesson.

So what I want you to do is match the method to the description.

Pause the video while you have a go at that.

Let's take a look then.

So readline reads a single line from the text file.

readlines reads all the lines and places them into a list.

strip removes the \n from the end of the string.

read reads the entire text file and close is to be used when you are finished accessing the text file 'cause it closes it.

Fantastic then.

So now you've learned how to read a text file within your Python programmes and you've done some challenges as well with that.

And we'll look at text files a bit further in our next lesson.

If you'd like to, please ask your parent or carer to show your work on Instagram, Facebook or Twitter, tagging at @OakNational and #LearnwithOak so that I can see what you've been getting up to in this lesson.

And I'll see you again soon for lesson four.