Loading...
Appending to a text file
- Key Stage 4
- Year 11
- Computer Science
Okay, we're now going to have a look at the append method instead. So again, I have opened the Raspberry Pi Code Editor and I've written the lines of code exactly the same as they appear in the slides. So we have on line 1, file = open and we're opening the text file players.txt and then we have a comma and this time we're using the append mode so we have "a" in speech marks. On line 2, I have a write statement, so I have file.write and in my brackets and speech marks I have the name Aisha. And then on line 3, I have my file.close() to close the file once I have written to it. Now if we just remind ourselves what was in players.txt. So if you remember, we originally entered the name Aisha and then we overwrote it with the name Jun. So at the moment, players.txt contains just one name, which is Jun. Now if we go back to the programme and we run the programme and then go back to players.txt, we can see that Aisha has been added to the file. So rather than replacing or overwriting the original content, we've appended that name to the end of the file. Now the Raspberry Pi Code Editor is quite kind because it has automatically put Aisha's name on a new line for us. Some IDEs will not do that for us, so what we have to do in that case, so for example, you may have gone into your text file and you may have found that Aisha was actually on line 1 right next to the word Jun. If that's the case, what you have to do is you have to use the new line character, which remember is \n before you write the name, and what that will do is it will put this down onto a new line. You don't have to do that in the Raspberry Pi Code Editor because it already does that for us.
Appending to a text file
- Key Stage 4
- Year 11
- Computer Science
Okay, we're now going to have a look at the append method instead. So again, I have opened the Raspberry Pi Code Editor and I've written the lines of code exactly the same as they appear in the slides. So we have on line 1, file = open and we're opening the text file players.txt and then we have a comma and this time we're using the append mode so we have "a" in speech marks. On line 2, I have a write statement, so I have file.write and in my brackets and speech marks I have the name Aisha. And then on line 3, I have my file.close() to close the file once I have written to it. Now if we just remind ourselves what was in players.txt. So if you remember, we originally entered the name Aisha and then we overwrote it with the name Jun. So at the moment, players.txt contains just one name, which is Jun. Now if we go back to the programme and we run the programme and then go back to players.txt, we can see that Aisha has been added to the file. So rather than replacing or overwriting the original content, we've appended that name to the end of the file. Now the Raspberry Pi Code Editor is quite kind because it has automatically put Aisha's name on a new line for us. Some IDEs will not do that for us, so what we have to do in that case, so for example, you may have gone into your text file and you may have found that Aisha was actually on line 1 right next to the word Jun. If that's the case, what you have to do is you have to use the new line character, which remember is \n before you write the name, and what that will do is it will put this down onto a new line. You don't have to do that in the Raspberry Pi Code Editor because it already does that for us.