Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

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.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
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.

Back to the top