- [Craig] In this video, we take a look at the use of basic file handling operations
(uplifting piano jingle)
So to explore the various operations you need to be able to use,
we're going to use this program you see on the screen,
and we're going to start with how you read data from text files.
So this program asks for the name of a character,
and then it looks up the name of that character in a text file,
and it outputs all of that character's attributes,
in this case their health, their stamina and their hunger, which are integers.
So you can think of this as a little text-based game.
You can see on the left the code for this program,
and you can see on the right the text file that stores the information.
You can see it stores the information in a very specific way.
We have the name of a character, Boris, followed by three numbers, 34, 57 and 12,
which represent the health, stamina and hunger for the character Boris.
Our program, as you can see, requires the items from the text file to be in a very certain order.
We require the name to be in the text file first, followed by three numbers,
and those numbers have to be in the right order.
The first one's the health, the next is the stamina and the next is hunger.
This structure that's very precise and has to be followed in the text file
is known as data dependency.
If the order of these were changed around in the text file,
the computer program would give undesired output or may not even work.
So first we ask the user which character they would like to output.
So we have a simple input command, which character you'd like to output.
They type in the name of the character and that gets stored in the variable character.
We then reach the first of our file handling operations
that you need to be aware of for the exam.
And that's open.
So we have a line here that says
F equals open characters dot TXT comma R.
And you can see the open command takes two parameters.
The first is the name of the file it wants to open,
in this case, characters dot TXT.
The second parameter it takes is what we want to do with the file.
So in Python, when you say R, it means we want to open the file for reading.
The next thing we do is set a Boolean variable to false
to indicate that we haven't yet reached the end of the file.
And this is important.
All text files and file handling operations
have a way of telling when they've reached the end of a file.
And it's quite common to check whether we've reached the end.
We then iterate through the file while we haven't reached the end of the file.
Now, we will change this end of file flag to true later on
if we discover we have reached the end of the file.
We then have four lines of code which read four lines from the text file, one after the other.
Each line from the text file is placed into an appropriate variable.
So we see name equals F, and that was assigned earlier as being a pointer to characters dot TXT.
F dot read line.
So read a line from characters dot TXT and place it into the variable name.
You'll notice on the end it has this dot strip, which is a Python thing.
And that's because every time you were entering a line of text into the text file,
at the end, you were pressing enter.
Now although when you press the enter key on a keyboard, you don't see anything,
behind the scenes,
it's actually storing a character,
which represents the fact there's a new line.
So we strip this new line character out so we just get the text, Boris.
We proceed with that for all four lines,
reading in the three numbers
and storing each line of that text file in health, stamina and the variable hunger.
We then now check if the character we read in from the file is indeed the one we're looking for.
So was the name supplied at the beginning of this program Boris?
And if it was, it matches,
we then print out the contents of the variables.
We've essentially extracted them from the text file,
put them into variables which are being held in memory
and then printed the contents of those variables out to the screen.
If the name we read in contains no data,
then we know we've reached the end of the file.
And if this is the case,
we set our end of file flag to true,
which will cause the while loop to terminate.
And then finally, we close the file.
So we say F dot close.
Remember, F is pointing to characters dot TXT.
It's our link, it's our file handler,
which is connecting our program to our text file.
So we say file dot close.
Now, this is really important and a step that candidates often forget in the exams.
It's also often a step that will gain you an easy mark.
Once you're finished dealing with a file,
you should close it and free up the resources.
So that's reading from a text file.
But how do you use file handling operations to write to a text file?
Well, we've got a slightly different program here.
The first thing you can see is we get the user to input the details for a character,
and we store those in variables in memory.
So we ask the user for the name, health, stamina and hunger of the character,
and we're storing the values entered into four variables in memory.
The next thing we have to do is to create a connection or a link
between our program and our physical file, characters dot TXT.
So like before, we say F equals open,
and then we supply two parameters,
the name of the file we want to connect to,
in this case, characters dot TXT.
And now we're supplying the second parameter, A,
and A means we want to append or add to the end of that file.
Another option in Python is to use the parameter W.
Now this would create a brand new file or overwrite the contents of an existing file.
Now we've gathered the input from the user
and we've created the link to characters dot TXT
and told it we want to append to the end of the file,
we can now write the contents of each variable held in memory
onto a new line into the character text file.
And you can see we do this with F dot write.
You'll notice I'm writing a variable
plus slash N,
and that is a new line character.
So it's as if you had manually entered the information and then pressed enter.
Just like when we read to the file,
once we're done, we close the file,
and you do F dot close.
So let's just take a step back for a moment
and actually look at the complete list of file handling commands
that you could be expected to use in the paper two on-screen exam.
Pause the video and take some notes from this table.
Now we've already gone through the three here for how to open a file for reading
and how to open it for writing and appending.
We've also gone through how to close a file.
And we've also already gone through in the previous examples
how to read lines from a file and how to write to a file.
There are three additional lines though,
which you're required to know in the table, which we didn't cover in the previous example.
So we're just going to briefly step through those now.
The first is the concept of using a for loop, for line in file ID,
which we can use to read every line from a file one at a time.
And here's an example of how we would do that.
So we can see we have a text file called my file,
and it contains various lines of text,
Craig, Dave, Sam, Mark, et cetera.
So I say my file equals open,
so my handle or my variable here is going to be called my file,
and I'm opening the file,
my file dot TXT,
in read mode, as we did earlier.
Then we're saying for line,
and we can write anything there,
for line in my file, print line dot strip.
And that takes out each line from the file,
strips the new carriage return off the end
and prints it to the screen
and goes through the file, the text file, one line at a time,
printing each line to the screen,
and then eventually, obviously, we close the file.
We can also use an interesting method here.
We can say a list equals file ID dot read lines,
and it's important this is read lines with an S on the end and not read line.
And this returns a list where each item is a single line from the file.
So again, here we have our original text file, my file dot TXT,
and it has a number of lines,
each containing names.
Again, we open the file in read mode.
We've then declared an empty list,
A equals square brackets,
and then we're going to say that list becomes equal to
my file dot read lines, open and close brackets.
If I now print the contents of A, which is a list,
you can see that each line from the text file has been pulled out separately
and been put into a separate item in a list.
Notice because I did read lines, it's pulled out each line,
Craig, Dave, Sam,
but also the hidden character on the end,
which is the character return, slash N.
In a similar way, we can use write lines,
so that's with an S on the end,
and we can write a structure such as a list to a file.
So I've started off here by creating a list called new list,
and that has four items in it, Stephen, Sarah, Claire and Tony.
And notice how within the strings,
I've had to add slash N because when I write the contents of this list to my text file,
I want it to enter the hidden character of a character return, a carriage return,
in other words, pressing enter.
I'm then going to open myfile dot TXT in append mode,
and now I can simply say myfile dot write lines,
and I give it the list new list and then close.
And as you can see, if I open my text file,
it's taken the contents of the new list list
and written each of the items in that list to a new line.
So just to recap, the stages to write data to a file are
open the file for creating or overwriting or appending to a file,
write the data to the file
and close the file.
The stages to read data from a file are
open the file for reading,
assign a Boolean variable to false to indicate the end of the file is not reached,
and while the end of the file is false and the search item is not found
then read the data from the file.
If the data matches what has been searched for,
assign the data to variables or output.
Check if the end of the file has been reached,
and if it has, assign the Boolean variable to true.
And again, close the file.
So that's the theory content of the video covered.
We're just going to spend a little bit of time talking about the programming language subset.
If you've heard this before,
you can skip the rest of this video.
Otherwise, it's definitely worth a quick watch.
So the exam board Edexcel have released a document that they call the programming language subset,
which you can see here on the right.
This document specifies which parts of Python 3 are required
in order that the assessment can be undertaken with confidence.
Students familiar with everything in this document
will be able to access all parts of the paper two on-screen assessment.
The exam board will not go beyond the scope of this document
when setting you any questions in the paper two exam.
Any students who do successfully use more complex methods which are not included in the document
will still, however, be awarded marks.
Further to this, you will see here from the front of the exam paper
that you are actually going to be supplied with a printed and an electronic copy of the PLS
during your paper two exam.
Well, if this document outlines everything you need to know
in order to program in Python during the exam
and you can be guaranteed that you can answer all the questions
using all the techniques in this document for full marks,
then you might as well get used to using it now.
Ask for a copy from your teacher,
and have it as a reference guide at all times throughout your GCSE course.
It will be invaluable.
(uplifting piano jingle)