Loading...
writing to a file
Key Stage 4
Year 11
Computer Science
Okay, we're gonna do some live coding now to see how we create a new file and write some contents to the file. So I've opened the Raspberry Pi Code Editor and I have copied across exactly the same line that we have seen in the slide deck. So we've got file = open, open brackets and then we have the name of the file, which is, "players. txt" and then a comma. And then we have "W," which is the file handling method, which we are using, which is the right method in this case. So we are saying, "Open the file called, 'players. txt'." If it is not created already, then it will create that file and we are going to write some new contents to it. Now, if I hit run in the Raspberry Pi Code Editor, (mouse clicking) nothing will happen here. If you are using a different IDE, for example, you may have an ID installed directly on your machine. You may find that when you hit run with this line of code, that actually the text file, 'players. txt' is created and it just creates a blank file, at this point. In the Raspberry Pi Code Editor, the file won't be created until we write something to it. So that behaves perhaps slightly differently to some other IDE's. (mouse clicking) So, what I'm going to do is I'm going to add a new line of code this time (keyboard clacking) and I'm going to write, "file. write," and I am going to add the name of the first player in speech marks, which is going to be, "Aisha." Okay? Now it's always good practise to do a, "file. close," (keyboard clacking) and in some IDE environments, if you don't write the line, "file. close," then you'll find that actually the text isn't written to the file. So it's important to add that in. So if I hit run now, hopefully we will see (mouse clicking) that in our project files on the left hand side we have a text file which has been created, which is called, "players." And if I click on
(mouse clicking) that text file to open it, we can see that the name Aisha has been written to the file. Now, if I go back to my code
(mouse clicking) and I change Aisha's name to, "Jun," (keyboard clacking) and I run the code again,
(mouse clicking) if we go back to, "players. txt," (mouse clicking)
we can see that because we are using the right mode that the original name that we added, which was Aisha, has been overwritten (mouse clicking)
and we now have the name Jun in the file instead. So we have to be careful when we're using the right mode because it will overwrite the original contents of the file.
writing to a file
Key Stage 4
Year 11
Computer Science
Okay, we're gonna do some live coding now to see how we create a new file and write some contents to the file. So I've opened the Raspberry Pi Code Editor and I have copied across exactly the same line that we have seen in the slide deck. So we've got file = open, open brackets and then we have the name of the file, which is, "players. txt" and then a comma. And then we have "W," which is the file handling method, which we are using, which is the right method in this case. So we are saying, "Open the file called, 'players. txt'." If it is not created already, then it will create that file and we are going to write some new contents to it. Now, if I hit run in the Raspberry Pi Code Editor, (mouse clicking) nothing will happen here. If you are using a different IDE, for example, you may have an ID installed directly on your machine. You may find that when you hit run with this line of code, that actually the text file, 'players. txt' is created and it just creates a blank file, at this point. In the Raspberry Pi Code Editor, the file won't be created until we write something to it. So that behaves perhaps slightly differently to some other IDE's. (mouse clicking) So, what I'm going to do is I'm going to add a new line of code this time (keyboard clacking) and I'm going to write, "file. write," and I am going to add the name of the first player in speech marks, which is going to be, "Aisha." Okay? Now it's always good practise to do a, "file. close," (keyboard clacking) and in some IDE environments, if you don't write the line, "file. close," then you'll find that actually the text isn't written to the file. So it's important to add that in. So if I hit run now, hopefully we will see (mouse clicking) that in our project files on the left hand side we have a text file which has been created, which is called, "players." And if I click on
(mouse clicking) that text file to open it, we can see that the name Aisha has been written to the file. Now, if I go back to my code
(mouse clicking) and I change Aisha's name to, "Jun," (keyboard clacking) and I run the code again,
(mouse clicking) if we go back to, "players. txt," (mouse clicking)
we can see that because we are using the right mode that the original name that we added, which was Aisha, has been overwritten (mouse clicking)
and we now have the name Jun in the file instead. So we have to be careful when we're using the right mode because it will overwrite the original contents of the file.