video

Lesson video

In progress...

Loading...

- Hi, I'm Rebecca you're 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.

It's good idea to have some pen and paper handy to answer any of the questions that I give you in the lesson and also please try your best to make sure that you've removed as many distractions as you can 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 write to text files and append to text files.

Think back to what you learned in last lesson then, and see if you can make a prediction about what will happen with this programme.

Pause the video while you do that.

Let's take a look then.

So an error will occur, but why will an error occur? Well, once the file has been closed, it can no longer be read.

An error will occur when the programme tries to access a closed file.

So when should you close a file? Pause the video while you think about that.

You should close a file when you were finished accessing or modifying it.

Why should you close a file when you are finished with it? You should closing a file, frees up system resources and protects the data.

We're now going to be lookin' at writing to a new file.

So, if you remember, there are four modes for opening up files.

You've got r for read, w for write, a for adding to or append, and r+ for reading and writing.

When we open a file in write mode, it creates a new file with the specified name.

If that file already exists, then the file will be overwritten, so very, very important to remember that one.

What I'm going to do then is I'm going to show you how you can write to files in Repl.

it.

So I've just gone to my Repl.

it account, and then I've just copied in that new bit of code, which is to open it in write format.

And at the moment you can see, there are no extra files over here, so there's nothing happenin'.

If I run the programme though, you can see now we've got a text file there called players.

txt, so that's what that little bit of code has done.

Now, if I want to write to that file now, let's just look in it first of all and just see, you can see that it's completely blank at the moment.

So if I want to write to that file, I can do file.

write, and then whatever I've put as the string here in the brackets, so I can put a player names.

So dinoshark would be a player name.

Now at this point, you might think, okay, if I run this, this is going to work, so I'm going to go to run.

It doesn't look like much has happened there.

Let's just look and see what happens to players.

txt.

Well, actually nothing has happened.

Now, have you got an any kind of inklin' as to why nothing's happened? What line of code have I missed? Pause the video while you think about that.

So I have missed the closing part, so I have to close the file because I've finished accessin' it or modifyin' it.

So I need to do close.

Now when I run the code, it should have dinoshark, which it does.

Now also, when we are writing new things to a text file, we need to make sure, before we were pulling it in, it had that backslash n into it as well.

If I want a new line here with a new string, I'm going to have to put backslash n in myself.

So if I want to put backslash n, I can do, and then I can put my new bit of information, so next one might be mousedragon.

And now when I run the code, you should see it's on a separate line, and you do.

So that's just a basic introduction into how writing to text files works in your Repl.

it account.

So now you've got an idea of how it works in Python, in Repl.

it.

What I want you to do is use the Writing to a New Text File section of your worksheet to try out some mini challenges.

So pause the video while you have a go.

Excellent, let's look at the solutions then.

So this was the Challenge One solution.

Remember, you might have done it slightly differently, and that's absolutely fine.

There's more than one way to solve this problem, so pause the video while you check your work.

Great, and this is Solution Two.

I've had to split it up over two sections, so if you look at the left side is the top of the code, and the right side goes directly underneath it.

It was just so that you could see it properly on the screen that I've had to split it in half there.

And again, more than one way to solve the problem, so yours might not be exactly the same.

Pause the video while you have a look.

Okay, now we're going to look at appending to a text file.

So when you append to a file, you are adding new data to the end of the text file.

The mode character for appending to a file is an a, and you can see that there.

Now there is not a separate append method for text files, so you still need to use the write method because you are writing to a file.

So what is missing from the programme for it to work correctly do you think? So the file must be closed in order for the programme to work as expected, and the file will remain open and won't be updated like we saw in that live code just a few moments ago.

Now what I'm going to do is I'm going to do a quick demonstration showing you how to append to a text file in our Repl.

it accounts.

Here is the code then from the slide deck, and you can see that it's now in append mode, not in write mode, so it's going to append to a file.

And let's just have a look at what the file looks like now, before I run the code.

So you've got Fred, Wilma, and Dino on three separate lines.

Now, if I go back to main and I run this programme, what do you think the text file is going to look like? So just pause the video while you have a little think about what it might look like.

I'm going to run it then, and let's just see if you are right or not, let's just see what happens.

So let's go to players.

Okay, so did you expect that? So what it's done is it's added dragonshark to the end of this line, dino.

So why do you think it's done that? It's because I haven't put backslash n at the beginning here.

'cause if I want it to go onto the next line, append to the next line, then I've got to make sure I put this backslash n.

So I'm just going to delete this, just so that I can show you, and I'm going to put backslash n there, and I'm going to run it again.

And hopefully this time it's going to work.

Yeah, it is.

It's gone to that new line there.

Now you've seen me append to a file, it's time for you to have a go.

So use the Appending to a Text File section of your worksheet to try some mini challenges.

Pause the video while you have a go at that.

Excellent, let's look at the solutions then.

So here is the solution to Challenge One and that disclaimer again, your code might be slightly different.

That's fine as long as it works.

So pause the video to check that code.

And then this one is Challenge Two, the solution.

Again, yours might be slightly different.

Pause the video while you check it through.

Fantastic, so you now know how to write to text files in Python.

So, we're getting quite far now into all these programming techniques that you can use, so getting closer to being able to do quite a challenging project towards the end of this unit.

Now, if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook, or Twitter, tagging @OakNational and #LearnwithOak, so I can see what you've been getting up to.

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