7.2 Reading Entire Files
Sometimes you don't want to bother with reading an entire file one line at a time. Sometimes you just want to read the whole thing and start processing it all at once. Well, you are in luck! Python has several different ways to read entire files, and in this lesson I'll share two common methods.
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
7.2 Reading Entire Files
In the previous lesson we started to talk about dealing with files. Reading files and then printing out the lines, each individual line to, the end user. But we took a couple shortcuts and just talked about the basics. And there's so many ways to process the contents of a file especially when it comes to text. That in this lesson I want to talk to you about a couple other very viable options. So, the previous lesson, we talked about creating a reference to a file, opening that file, and then iterating through that file one line at a time using a for loop. Now that's absolutely a decent option and it will work just fine, but depending on what you want to do with that file, maybe it's not the best idea. So there's other things that we can do with a file in order to get at its contents. So another way for us to work with this file is to read all the lines at once or read the entire file as a single string. Now these options all kind of depend on how large the file is. If you're dealing with large files like the book War and Peace or something you probably are not gonna want to read them all in one shot. Because it's gonna be way too big, it's gonna take up a lot of resources. But if you're dealing with smaller files and you know that you can open them in a decent way and store them in memory, then it's fine. But it's a good idea to understand how you can work through these processes. Now also in the previous lesson, I also showed you a simple way to open and close the file. But you had to open it and then you had to remember to use the close function as well. But you're gonna find that you're not gonna see a lot of people writing code that way. There's another way to do it and I'm gonna show you that now as well. So one way that we can circumvent having to use the open and close as separate lines cuz remember we've talked a lot in this course about condensing your code and writing fewer lines. There's a way to get around that. So, the way to get around that is using the with statement, so we can use with, we can specify that we want to open lorem.txt. And then once again, this is going to open it in read-only access, and then we can reference that as lorem_file, just like we did before. So, what I've done now is I've used this with statement that's going to open, that's going to execute this function. It's gonna open the file. It's gonna store a reference to that opened file, in lorem_file, just like we did before. And now, I can specify what it is I want to do with that file. The benefit to using the with statement, in this case is that when I'm all done with this indented block of Python code, it will automatically run close for me at the end. So I don't have to explicitly call close, which is nice for a couple of different reasons. Because as a lazy programmer, I don't want to have to write an extra line if I don't have to. And it's also kind of got my back a little bit in case I would forget to do it. It's gonna handle that cleanup for me which I think is a very nice way to go. So what I'd like to do now is I'd like to read that entire file in all at once in a couple of different ways. And then I can be able to process it however I might want. So the first thing that I want to try, is I want to be able to read all the files in. But I wanna keep that delineation of each individual line like I did before. But I wanna deal with it all in one way and not have to loop through it like I was doing before. And the way that you can do that is by using a function called read lines. So right now I have lorem_file. And I can say that my lines are gonna be equal to lorem_file.readlines. This is a function that's going to read the entire file in one shot. And it's going to take each individual line and store them as an entry in a list and return the list out here to lines. So once I've done that then I can say printLines like that, go ahead and save my file. And now remember I don't have to explicitly close this because it's going to close it for me once I've exited this block, this indented block of code within the with statement. So let's go ahead and save that, and we'll hop over toward terminal here, we'll clear this out. We'll say Python3. I'll say file_read_2 this time, and we'll go ahead and write that. So as you can see now, I have a list that was returned back to me. Each entry in that list contains a single line from that file. But I was able to read that file in one shot, I didn't have to loop through it. So now, I have access to each individual line in a list and I can go through and pick pieces apart. I could grab certain lines, I could iterate through these things, I could do all sorts of different operations. And I've got this nice compact data structure in which I can do all of this processing. So that's kind of nice. Now also what we can do is we can read the entire file in another way that's going to give us the entire contents of the file but this time not as a list but as an actual string. So how do we do that? Well, there is another function and you're going to find that this other function has a lot more uses as well. But I'm gonna stick to the basics for this time and I can just call read. Now read is a little bit of a Swiss Army knife when it comes to reading the contents of files within Python. But if you call just read, it's going to give you the entire contents of the file as a string. So at this point, maybe I'll just call this contents. And then I will copy this and print out the contents, so let's go ahead and save that. Hop back over to our terminal, we'll go ahead run this again. And as you can see now, I have printed out the entire file contents in one shot. So like I said before, you can grab an open reference to a file like we did in the previous lesson and then iterate through that file one line at a time and print it out to the end user or do some sort of processing on each line. Or whatever it is you would like to do, that's absolutely fine. But in other instances when you're dealing with small files with just a little bit of content in them. Where you're able to read all of the content into some variable into memory at the same time. Then this is a very fast way to do it by using either the read lines function which will read each individual line and store each one of them as an individual entry within a list that ultimately gets returned. Or you can use read which will read all of the contents into a single string that is then stored within memory that you can then print or do whatever it is you would like to do and these are great. Now I can read each individual line, I can read all the lines into a list, I can read the entire contents into a string. But what if I only want to read bits and pieces? What if I just want to read a certain number of bytes? What if I don't want to have to read the entire file? I just want to be able to read a certain word or go to a certain location within the file, and all those types of things. How can I do that? Do I have to read the entire contents and then find in there where it is exactly that I want to go? And the answer to all of that is no but I'm gonna show you how to do some of those operations in the next lesson.