In this video, Kathryn teaches you how to open and close files using Python's built-in functions, as well as some of the methods associated with the files themselves. She shows you how to create a file, look at some attributes of the file, and write to the file yourself in Python.
- Before we start messing with files in Python we actually need to create a file. And so here I'm going to go up to file, new, just creating a new file in this current directory where the Python file I'm running is. And I'm just going to call this new file scores.txt. And here it is, it's in that same directory and we are just going to keep this file blank. We're going to save it and we're actually going to mess with it inside of our Python code. To get access to this file we'll actually create a variable called myFile in here.
And then we'll go open, scores.txt, so the name of the file we want to open and then w for the mode that we want to open it in. And here w stands for write. If we put r here it would be read, r+ would be read and write, a for append, but we're going to keep it w for now. Once we open this file then it's going to be saved inside of this myFile variable, and that's what we'll use to access the scores.txt file in the future for this Python code.
Now that we have it open, we're going to go ahead and print out some of the attributes and properties of this file. And so we'll go print and we'll get the name of this given file in here to get the name we'll just go myFile.name. And then we can also get the mode of this file. And so we can say mode and then plus myFile.mode. And so these are just some properties, some attributes, some things you can access. In here, no surprise the name is scores.txt and then the mode is w.
With our file open we can go ahead and write to the file. And so to write to the file, we'll go myFile.write. And then we'll put in whatever we want to write to this file, so the scores.txt file. And in this case because it is storing scores, we'll give it some initials and scores that it will save for it's high scoring players. And so here we'll have GBJ, that's a player, and this player's score was 100 so they're the high score. And then say we have another high score whose name is KHD and then we'll have this person's score be 99.
And then we'll do another /n which I'll explain in a second but the last score that we'll have is BBB and the score will be 89. When this program runs the /n's will be converted into new lines and so these /n's are only here for formatting purposes. And so with the /n's this will look like GBJ and then a hundred and then KHD and the 99 et cetera. They're all going to be on separate lines. This'll make our file look super organized.
Once we've written things to the file, we'll go ahead and close it, and so we'll go myFile.close. But before we move on it's important to remember that when we're writing to a file it completely erases what's currently in the file and writes whatever is right here in these parentheses. And so if you want to add on to file and not erase everything that's in it currently you'd want to use the append mode versus the write mode. And so let's go ahead and run this. And nothing will change here but if we go to our scores.txt file this now has our scores inside of it.
And so they're saved and then if we run another program and that program accesses this file it will see these scores as well, as long as another file doesn't change them. Now we can actually read what we just wrote in the scores.txt file. All we have to do is go myFile equals open and then the file name which again will be scores.txt and then the mode and this time we're going to be in the reading mode so we're going to be in r. Then, we can go ahead and just read the file.
And so we'll go print, reading dot dot dot, and then we'll go ahead and go myFile.read and this will read what's in the file currently for us. Now here we just wrote over this myFile variable name. You could change this variable name to myFile2 and that would work as long as you change this to myFile2. And so you can use the same variable names over again, as long as you don't need what was in it previously. And so here we go we're going to read the file and it's going to read everything out for us.
We're reading, we see GBJ, the score, the score, and the score. If we only wanted a certain part of the file we could go, read 10, and this will only read 10 characters of the file. And so here we see the first score, but the other scores are still there, we're just not reading them. Now if I try to read again after this, say, print reading again and have the same line of code, myFile.read 10, print this out, notice it's starting at a different point in the file.
This has to do with the seek pointer which we're going to talk about in the next video.
- 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