Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Data python 3
  • Overview
  • Transcript

3.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.

3.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 short cuts 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 wanna talk to you about a couple other very viable options. So in 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 wanna do with that file, maybe it's not the best idea. So there's other things that we can do with the 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 wanna read them all in one shot, because it's gonna be way too big, it's going to 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 lessons, 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 of 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.text. 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, it's gonna 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 wanted 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 wanna try, is I wanna 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 have 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 to our 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's another function, and you're gonna 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've 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 wanna read bits and pieces? What if I just wanna 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.

Back to the top