[MUSIC] When you're working with text files it is pretty common to want to operate on the lines within the file. So instead of getting the entire contents, maybe I want see each line in the file. So we know we can read the whole file but then you would have to split it up into separate lines. Well, Python gives you a little bit more convenient way of doing this by allowing you to iterate over the lines in the file. Let's see how this works. File objects have a method called readLines that allows us to break the contents of the file up into lines. Now if you recall the read method gives us back the entire contents of the file as a single string. readLines in contrast gives us back a list of strings and each string in this list corresponds to a single line in the file. All right, how do we use it? Well, first we have to open the file as before. I'm going to open the the_idiot.txt again. And then we can call readLines on the file object that we have back. And because it's a list we can iterate over it. And each time through the for loop here we will have a different line of the file, so we can do whatever we want. Here I'm not going to do anything interesting. I'm just going to print out each line. And don't forget, always close your files. Okay, let's see what actually happens here. Let's see if we get file and print it out. All right, so we've seen this file before, so you hopefully recognize the text here. But things look a little bit different, right? Why are there all these extra blank lines? Well, readLines leaves the new line at the end of the string, so that when I print it out line-by-line, I'm printing out the line from the file, which includes the new line, and then the print function actually adds its own new line at the end of whatever it prints. So there are two new lines here each time we print. This is just an artifact that I'm printing the lines. If you were processing them, just be aware that at the end of the line there is the new line character. So if you don't want that, you need to get rid of it. And we know how to index into strings to find it and do other operations so we can get rid of it. readLines reads the contents of the entire file and breaks it up into lines and puts those lines into a list. This takes some time and if you have a very large file it could take quite a while. It also consumes a lot of memory holding that big giant list. So Python does provide us a different way of iterating over the lines of the file. And in some situations, especially for very large files, that can be a lot faster. So, I'm going to open the file again and then I can iterate directly over the file object. I don't need to call any methods. So, just like I can iterate directly over a list, I can iterate directly over a file object. And what this means is you will get back lines from the file. It does not read it all at once, put it into a big giant list, it reads the next line out of the file and returns it to you. So we do the exact same thing here. Each line, I'm going to simply print. And I didn't forget to close my file. All right, let's see what happens here. It looks exactly the same. So, these are two equivalent ways of iterating over the lines of the file, but the second way does something different okay? It can be more efficient. When you're working with text files, you very frequently want to work with them on a line by line basis, you want to process the lines in the file, not work with the entire files, one big giant string. So you can break it up yourself after you've got the big giant string, but like we've seen here, there are better ways. Python does allow us to iterate in multiple ways over the file line by line. In most situations, you probably just want to iterate over the file directly and get the lines. You can call readLines though and get a list of strings. You don't have to iterate over that list of strings. You can do other things with that list of strings. So readLines is valuable both in the context of iteration and outside of the context of iteration. If you have large files, though, you probably do not want to try and read the entire file at once. Instead you want to iterate using the second method, iterate over the file object, so that you're getting one line at a time and not creating this big giant list.