[MUSIC] In this video, I'm going to talk about opening and reading files in Python. You do have to worry about where the files are, but in this video, I'm going to largely ignore that issue. And we're going to only work with files that are in the exact same directory as the Python program. So we're just going to give the file name, we're be able to open it easily then and not have to deal with any issues related to paths and directories. Okay, let's take a look at how we open and read files. First, let's take a look at how to open files and for this purpose, Python has the intuitively named open function. So, that's what we're going to use. Open takes two arguments. The first argument is a file name, which is string, and this names the file. In general, that can include paths, but as I said for this video we're just going to use the file name itself. And we're only going to work with files that are in the exact same directory as the Python program itself. So here you can see that my file name is the_idiot.txt. [LAUGH] All right, interesting file name. The second argument is what is called the mode, and this is also a string. Now, because we're going to just read files, the interesting characters that can be in the mode string here include r, r means open this file for reading, okay? And then we can also add either a t or a b, but not both, okay? T means text and b means binary, so we're telling Python, do we want to open this in text mode or binary mode? The differences are somewhat subtle, but basically a text file is simply just lines of text. It's something that you might have typed into a very plain, basic text editor. And when you tell Python to open it in text mode it's going to assume that the file is lines of text. And it might do some special things to convert some characters to make that work out nicely. If you open a file in binary mode, it will do nothing special. It will just allow you to read exactly what is in the file without changing anything for you. And this is important when you have files that contain data that is being interpreted directly by the program, and is not necessarily text. Maybe you're saving something else in there. So we call open with these two strings, the file name and the mode and we get back well, an open file. So I'm going to assign it to a variable called open file, but what is that? It's not the contents of the file, you may think, hey I've now opened it and I have what's inside the file. You do not, you have a file object. So let's print out the type of this thing and print it out and see what Python tells us, okay? Now importantly, when you're done with a file, you have to close it. If you just leave files open, interesting things can happen. I'm not going to go into all of the potential problems here. But just remember, whenever you're done reading, or writing, or doing whatever you're going to do to the file, make sure that you close it. Now let's run this and see what happens. Okay, so you can see that when I printed the type of open file it says something _IO.TextIOWrapper. Okay, I don't really know what that is, but it's certainly not the contents of that file. If I just print the file, the openfile variable itself, it tells me, well, it's that class. It's the name of the file is in there, and the encoding. What character set they're using for this text file. So the file the_idiot.txt was actually there, so I was able to open it. But what happens if the file's not there? So let's look at a situation where I try to open something that isn't actually there, there is no file. All right, so I'm going to call open with nosuchfile.txt as the file name, and you'll have to take my word for it, that's not there. And you'll notice also though that I didn't give a mode string to open. If you don't give open a mode stream, the default is to open the file in text format for reading. Now I don't recommend not giving a mode. You should be explicit about what you're trying to open the file for. But in this case, it doesn't matter, the files not there, we're just trying to figure out what's going to happen when I do this. So, let's find out. Python gives an error, okay? It says, file not found, no such file or directory. Now depending on the situation, you might get slightly different errors. It's possible that you are trying to write to a file that you're not allowed to or something like that, okay? So you should make sure here that the file that you're opening does exist and that you have the permissions to do what you would like to it, reading or writing. It's not very interesting to just be able to open a file, I actually want to be able to read the contents. That would be the whole point of using files here. So, let's take a look at how we can do that after we have successfully opened the file, how do we read the contents? So you can see here again, I'm going to open up the_idiot.txt and I get back from open, a file object. And these file objects have methods that I can use to access the contents. And one of them is read, and read returns the contents of the file to me. So let's take a look at how it does that. Let's print out the type of what got returned, and the length of what got returned, and then let's print it out. Now don't forget, you have to close your file when you're done. So down here at the bottom I am closing the file. Now I could have done that right after the call to read, I don't need the file open anymore after I read it. So I didn't need it to be open while I was printing everything out, but at least I remembered to close it at some point. So let's see what happens here. So we find out that Python returns the data as a string. That makes sense, I'm reading a text file, a string contains text. The length of it is 422 characters, so this was a relatively small file that had 422 characters in it. And then we print it out and see! Now I understand why I called it the_idiot, it is a paragraph from Dostoyevsky's The Idiot. So you now have the contents of the file as a string. You can operate on that string just like it were any other string and do what you need to do within your program. Reading files is a three step process, first, you need to actually open the file. So you need to have the file name, and you need to tell Python the correct mode that you want to open it for reading. Second, you take the file object you received back from Python and you call its read method, that will get you the contents of the file. And finally, don't forget, always close the file when you're done. Now the biggest potential pitfall here is errors when you try to open an incorrect file. So make sure that you have the right file name, just be careful there. Otherwise, the process is relatively straightforward.