In this video, learn how to read a file line-by-line and edit it if necessary. Kathryn demonstrates using the example of replacing a set of initials on a high score board.
- [Instructor] We've read files, we've written to files, but we can actually iterate the files by looking at just one line at a time. Here on the second line we have our scores.txt saved in this myFile variable. We can look at this file over here, and we have three lines in it, with each line having some initials and the respective score. Going back to the code, if we just want to read one line we can go something like, print, my one line, and then we'll go ahead and print out the line which here will be myFile.readLine.
And this will read one line of the file, and that one line is just that first line with the one set of initials and the score. Now read line does change the position of the seek pointer, and so we'll want to reset the seek pointer back to zero, and we can do this with what we learned in the last video with myFile.seek(0), so that way when we read again we'll be reading from the very beginning of the file. Because our file can be broken up into lines we can iterate through each line of that file.
And when we iterate, that means we can use a for loop and so we can say, for line in myFile, and we can do something here. And in this case, say we want to replace one of the high scorers, we can say newHighScorer = line.replace, BBB so we want to replace the initials of BBB with the new high scorer, PDJ, and say they have the same score as BBB which in this case was 89.
So here, we're replacing these initials. And the way we're going to do this is we're going to iterate through each line of the file. When we see BBB, we're going to replace it with PDJ. And once we replace it, we're going to go ahead and print out that new high score. Once we're done iterating through the file, we're going to go ahead and close it and so we'll go myFile.close, and we'll be done working with our file. So if we run this, notice we get each line printed out, but in this case, BBB has been replaced with PDJ.
Now if we go back to our scores.txt file, notice it was not replaced here. That's because we physically did not write to the file. If we wanted the score to actually change, we would need to use the right function for my.File. Here, we're just using the string line in replacing it within that given string. There's nothing connected to my.File. Although, you could connect it to that current file usually you want to save any new data in a new file, so that way it doesn't interfere with the old data you might still be reading.
The main advantage of using files and reading and writing and seeking is that it makes your data persistent. Meaning once the program ends, your data is still there. It's still in the file. And you can use this data in other programs that could be running in the background.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content